Home > OS >  How to create array list from for loop in react
How to create array list from for loop in react

Time:02-27

I am working on a project in VS built with react (I'm new to react) and I have a loop function that returns json data and I am trying to create a list/array - [1, 2, 3, 4] using the IDs from the loop -

for (const data of data.dataset) {
    ID = data.id;
}

How can I achieve this?

CodePudding user response:

The map function would be great for this.

CodePudding user response:

let myArray = [];

for (let data of data.dataset) {
    myArray.push(data.id)
}

console.log(myArray); // [1, 2, 3, 4]

You can use this method to create a new array with ID's.

  • Related