Categories
JavaScript Answers

How to create an array of object literals in a loop with JavaScript?

Spread the love

To create an array of object literals in a loop with JavaScript, we use the map method.

For instance, we write

const arr = oFullResponse.results.map((obj) => ({
  key: obj.label,
  sortable: true,
  resizeable: true,
}));

to call oFullResponse.results.map with a callback that returns an object with the properties and return an array with the objects returned by the map callback.

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 *