Languages
[Edit]
EN

JavaScript - map more than one property from array of objects

0 points
Created by:
Savannah
559

In this article, we would like to show you how to map more than one property from array of objects using JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const data = [
    { a: 1, b: 2, c: 3 },
    { a: 4, b: 5, c: 6 },
    { a: 7, b: 8, c: 9 },
];

const result = data.map(({ a, b }) => ({ a, b }));

console.log(JSON.stringify(result)); // [ {"a":1,"b":2}, {"a":4,"b":5}, {"a":7,"b":8} ]

 

Alternative solution

In this example, we use map() method with destructuring assignment to omit c property and extract the rest of properties from an array of objects.

// ONLINE-RUNNER:browser;

const data = [
    { a: 1, b: 2, c: 3 },
    { a: 4, b: 5, c: 6 },
    { a: 7, b: 8, c: 9 },
];

const result = data.map(({ c, ...rest }) => rest);

console.log(JSON.stringify(result)); // [ {"a":1,"b":2}, {"a":4,"b":5}, {"a":7,"b":8} ]

References

  1. Map - JavaScript | MDN
  2. Destructuring assignment - JavaScript | MDN

Alternative titles

  1. JavaScript - map multiple properties from array of objects
  2. JavaScript - select only a few fields from array of objects using map
  3. JavaScript - extract only a few fields from array of objects using map
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join