DEV Community

Discussion on: .map( ) .forEach( ) for( ). 👉 Three ways for create an array and push it:

Collapse
 
anders profile image
Anders • Edited

Hi Luca, may I suggest changing the 3rd alternative to the following, I think you can do without the temporary variables there:

var newArray = [];

for (var i = 0; i < array.length; i++) {

    newArray.push(array[i].name);
}

Also, I'd recommend that you recommend that as the preferred version as it is also the fastest (the map version is 6.9% slower than a normal for loop, the forEach one is 25% slower). This has no bearing on this example with 2 entries, but getting people into the habbit of using the faster choice will benefit us all in the long run.

Collapse
 
ljnce profile image
Luca

Thank you Anders, your reply is awesome!