Array ES6 method (the reduce)

 Application scenario a sum of all the values ​​in the array
var Numbers = [3,5,9 ];
         var sumValue = numbers.reduce ( function (SUM, Number) { // before sum2 two numbers and 
            the console.log (SUM) // 1000 1003 1008 
            return SUM + Number; 
        }, 1000)   // first initialized with the array 1000 + the first item 
        the console.log (sumValue) // 1017
 
The properties of objects in an array to another array detached
 var primaryColors = [
            {color:"red"},
            {color:"yellow"},
            {color:"bule"}
        ]
        var colorsArr = primaryColors.reduce(function(arr,primaryColor){
            arr.push(primaryColor.color);
            return arr;
        },[]);
        console.log(colorsArr)    //["red", "yellow", "bule"]

 

Guess you like

Origin www.cnblogs.com/webmc/p/11622441.html