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 }
        ]

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)    

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)

Comments

Your Answer

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.