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

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.