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

vue routing global guard beforeEach and afterEach

 Global routing front guard (beforeEach) This function is used the most. Its function is to perform permission-related verification before routing jumps. This function contains three parameters: to: the object of the target route that is about to enter; from: the route that the current route is leaving; next: confirm the release. It can be used to log in and register, to determine whether there is a token before logging in, and release if it exists. , if it does not exist, it will not be released. The post routing guard (afterEach), its role is to trigger after the routing jump.

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.