Sometimes, we want to iterate through an existing array and add the items to a new array in Node.js and JavaScript.
In this article, we’ll look at how to iterate through an existing array and add the items to a new array in Node.js and JavaScript.
How to iterate through an existing array and add the items to a new array in Node.js and JavaScript?
To iterate through an existing array and add the items to a new array in Node.js and JavaScript, we call the array map
method.
For instance, we write
const array = calendars.map((item) => item.id);
console.log(array);
to call calendars.map
with a callback to return the id
property value of each item
in the calendars
array.
Then we put the values into a new array and return it.
Conclusion
To iterate through an existing array and add the items to a new array in Node.js and JavaScript, we call the array map
method.