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:
xxxxxxxxxx
1
const data = [
2
{ a: 1, b: 2, c: 3 },
3
{ a: 4, b: 5, c: 6 },
4
{ a: 7, b: 8, c: 9 },
5
];
6
7
const result = data.map(({ a, b }) => ({ a, b }));
8
9
console.log(JSON.stringify(result)); // [ {"a":1,"b":2}, {"a":4,"b":5}, {"a":7,"b":8} ]
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.
xxxxxxxxxx
1
const data = [
2
{ a: 1, b: 2, c: 3 },
3
{ a: 4, b: 5, c: 6 },
4
{ a: 7, b: 8, c: 9 },
5
];
6
7
const result = data.map(({ c, rest }) => rest);
8
9
console.log(JSON.stringify(result)); // [ {"a":1,"b":2}, {"a":4,"b":5}, {"a":7,"b":8} ]