Map

//ES5 Solution: 

var numbers = [1,2,3];
var doubleNumbers = []; 

for (var i = 0: i<number.length; i++){
    doubleNumbers.push(numbers[i] * 2); 
}

console.log(doubleNumbers); -> [2,4,6]

//ES6 Solution

//Each number in the numbers array is being passed in the anonymous function 
//What the function returns is placed in a new array
var doubled = numbers.map(function(number){
    return number * 2; 
}); 
console.log(doubled); -> [2,4,6]


//Another example:
var cars = [
    {model: 'Toyota', price: 'CHEAP'},
    {model: 'Mazda', price: 'EXPENSIVE'}
]

var prices = car.map(function(car) {
    return car.price; 
}); 

console.log(prices); -> ['CHEAP', 'EXPENSIVE'];

results matching ""

    No results matching ""