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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.