Reduce the use of methods

  reduce (Convergence): receiving a callback function as an accumulator, each value (left to right) in the array begins to reduce, to a final value, the new ES5 is still another processing method itemized array.

  reduce(callback,initialValue)

  callback (one array on each of a function call, the function accepts four :)

    • previousValue (a return value of the callback function, or the initial value)
    • currentValue (the current array element being processed)
    • the currentIndex (superscript array element currently being processed)
    • (Array call reduce () method) array

               (Optionally the passed value previousValue initial value. As the first call to the callback function) the initialValue

// array of substantially summing 
        the let of arr1 = [1,2,3,4,5,6,7]; 
        the let arr.reduce = SUM ((pre, Next) => { 
            return Next pre +; 
        }); 
        Console .log (sum);

// sum required for the array of objects on the second parameter to 
        the let of arr1 = [{COUNT:. 3,. Price: 2}, {COUNT:. 1,. Price:. 5}, {COUNT:. 5,. Price:. 1 } ]; 
        the let SUM = arr.reduce ((pre, Next) => {
             return pre next.count * + next.price; 
        }, 0); // 0 as a first pass pre value, corresponding to the array before 0 the insert element 
        console.log (sum);

 

Guess you like

Origin www.cnblogs.com/angle-xiu/p/11409303.html