Categories
JavaScript Answers

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

Spread the love

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

For instance, we write

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

to call results.map with a callback that returns an object with the original object obj‘s properties spread into a new object and we add the active property to it.

Then an array is returned with the original objects in results with the active property added.

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 *