Categories
JavaScript Answers

How to iterate through an existing array and add the items to a new array in Node.js and JavaScript?

Spread the love

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.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.