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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *