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

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 mappedResults = results.map((obj) => ({ ...obj, active: false }));

to call results.map with a callback that returns new objects that has properties in the existing object being processed and the active property added to it.

A new array with the new objects are returned so we assign the returned array to mappedResults.

Conclusion

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