0

how to append/push a new object in angular.

here's the data:

 data = [
 { title: 'Book1' },
 { title: 'Book2' },
 { title: 'Book3' },
 { title: 'Book4' }
 ]

What I want to do is to add inside the object like access. expected output:

data = [
 { title: 'Book1', author: false },
 { title: 'Book2', author: false },
 { title: 'Book3', author: false },
 { title: 'Book4', author: false }
 ]
asked Feb 27, 2020 at 6:59

2 Answers 2

2

You can add the property using a map and object destructor in ES6

data = [
 { title: 'Book1' },
 { title: 'Book2' },
 { title: 'Book3' },
 { title: 'Book4' }
 ].map(d => ({ ...d, author: false }));

let data = [
 { title: 'Book1' },
 { title: 'Book2' },
 { title: 'Book3' },
 { title: 'Book4' }
 ].map(d => ({ ...d, author: false }));
 
console.log(data) 

answered Feb 27, 2020 at 7:03
Sign up to request clarification or add additional context in comments.

Comments

1

You Can use Array.prototype.map()

Example :

 data = [
 { title: 'Book1' },
 { title: 'Book2' },
 { title: 'Book3' },
 { title: 'Book4' }
 ]
 
 data.map(item => {
 item.author = false
 })
 console.log("Data",data)

answered Feb 27, 2020 at 7:04

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.