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

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.