EN
JavaScript - return array of objects with new keys
0 points
In this article, we would like to show you how to return an array of objects with new keys in JavaScript.
In this example, we use the map()
method to return an array of objects with changed id
values.
xxxxxxxxxx
1
const users = [
2
{ id: 1, name: 'Kate' },
3
{ id: 2, name: 'Ann' },
4
{ id: 3, name: 'Tom' },
5
];
6
7
const result = users.map((user) => ({ id: user.id + 1, name: user.name }));
8
9
console.log(JSON.stringify(result, null, 4));
Output:
xxxxxxxxxx
1
[
2
{ id: 2, name: 'Kate' },
3
{ id: 3, name: 'Ann' },
4
{ id: 4, name: 'Tom' }
5
]