EN
JavaScript - return object with map() method
0
points
In this article, we would like to show you how to return an object with map() in JavaScript.
Practical example
In this example, we present how to return objects with incremented quantity values using the map() method.
// ONLINE-RUNNER:browser;
const items = [
{ id: 1, quantity: 10 },
{ id: 2, quantity: 20 },
{ id: 3, quantity: 30 },
];
const itemsIncremented = items.map((item) => ({
id: item.id,
quantity: item.quantity + 1,
}));
console.log(JSON.stringify(itemsIncremented, null, 4));
Output:
[
{ id: 1, quantity: 11 },
{ id: 2, quantity: 21 },
{ id: 3, quantity: 31 }
]