EN
JavaScript - map more than one property from array of objects
0
points
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} ]