map()
The map()
method creates a new array with the results of calling a provided function on every element in the calling array.
var new_array = arr.map(function callback(currentValue, index, array) {
// Return element for new_array
}[, thisArg])
map() vs forEach() vs for loop
var tasks = [
{
name: 'Write for Envato Tuts+',
duration: 120
},
{
name: 'Work out',
duration: 60
},
{
name: 'Procrastinate on Duolingo',
duration: 240
}
];
let tmp = [];
for (let i = 0; i < tasks.length; i++) {
tmp.push(tasks[i].name);
}
let tmp = [];
tasks.forEach((task) => tmp.push(task.name));
let tmp = tasks.map(task => task.name);