Categories
JavaScript Answers

How to add property to an array of objects with JavaScript?

Spread the love

Sometimes, we want to add property to an array of objects with JavaScript.

In this article, we’ll look at how to add property to an array of objects with JavaScript.

How to add property to an array of objects with JavaScript?

To add property to an array of objects with JavaScript, we can use the array map method.

For instance, we write

const newResults = results.map((obj) => ({ ...obj, active: "false" }));

to call results.map with a callback that returns the object we want to return by copying the existing object obj and add the active property to it.

Then we assign the array of mapped objects returned to newResults.

Conclusion

To add property to an array of objects with JavaScript, we can use the array map method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *