Languages
[Edit]
EN

JavaScript - return object with map() method

0 points
Created by:
GamerGoals22
364

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 }
]

References

  1. Array.prototype.map() - JavaScript | MDN

Alternative titles

  1. js map method return object
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