Skip to main content

Detailed explanation of reduce method in Js array

 The reduce method contains two parameters, the first parameter is a callback function, which contains four parameters, the second parameter is an optional parameter, as the first parameter of the callback function. It executes the callback function for each element in the array in turn, excluding elements that are deleted or never assigned in the array, and accepts four parameters: the initial value (or the return value of the last callback function), the current element value, the current Index, the array on which reduce is called.

See below for a practical example: Array summation second with initial value and without

 //
    <script>
        var x=[0,1,2,3,4].reduce((beginvalue,value,index,arr)=>{
            return beginvalue+value;
        });
        console.log(x)//10
        //
        var x1=[0,1,2,3,4].reduce((beginvalue,value,index,arr)=>{
            return beginvalue+value;
        },100);
        console.log(x1);//110
    </script>

Object array summation

var arr=[0,{score:100},{score:200},{score:300}];
        var sum=arr.reduce((beginValue,value)=>{
                return beginValue+value.score;
            });
            console.log(sum);//600

Convert 2D array to 1D array

 var arr=[[0,1],[2,3],null,,[4,5]];
        var arr1=arr.reduce((beginValue,value)=>{
            return beginValue.concat(value);
        },[])
        console.log(arr1);//[0, 1, 2, 3, null, 4, 5]
        //
        var c=arr.flat();
        console.log(c);//[0, 1, 2, 3, null, 4, 5]
        //
        var d=[].concat(...arr);
        console.log(d);//[0, 1, 2, 3, null, undefined, 4, 5]

Count the number of occurrences of the same element in an array

var arr=["aaa","bbb","ccc","aaa","bbb","aaa"];
        var obj=arr.reduce((beginValue,value)=>{
            if(value in beginValue){
                beginValue[value]++;
            }
            else{
                beginValue[value]=1;
            }
            return beginValue;
        },{});
        console.log(obj);//{aaa: 3, bbb: 2, ccc: 1}

Classify objects by attributes

var obj=[
            {name:"aaa",score:100},
            {name:"aaa",score:80},
            {name:"bbb",score:100},
            {name:"ccc",score:90},
            {name:"ddd",score:80}
                ];
        function fun(objectS,proto){
            return objectS.reduce((beginValue,value)=>{
                var key=value[proto];
                if(!beginValue[key]){
                    beginValue[key]=[];
                }
                beginValue[key].push(value);
                return beginValue;
            },{})
        };
       var result=fun(obj,"score");
       console.log(result);



Comments

Popular posts from this blog

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.