Categories
JavaScript Answers

How to apply a function to each object in a JavaScript array

Spread the love

To apply a function to each object in a JavaScript array, we use the map method.

For instance, we write

const newArray = oldArray.map((e) => {
  e.data = e.data.split(",");
  return e;
});

to call oldArray.map with a callback that sets the e.data property in the oldArray to a new value.

Then we return new object e.

An array with each object e in oldArray modified is returned.

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 *