SoFunction
Updated on 2025-02-28

Five iteration methods and two merging methods for js arrays (recommended)

Five iteration methods and two merging methods for js arrays (recommended)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Untitled document</title>
<script>
  = function(){
    //every() is equivalent to logic and    var arr = [1,2,3,4,5,6,7,8];
    var everyRes = (function(item,index,array){
        return (item>2);
    });
    alert(everyRes);
    //some() is equivalent to logical or    var someRes = (function(item,index,array){
        return (item>2);
    });
    alert(someRes);
    //filter() returns an array of given conditions    var filterRes = (function(item,index,array){
        return (item>2);
    });
    alert(filterRes);
    //map() returns an array of given conditions    var mapRes = (function(item,index,array){
        return (item*2);
    });
    alert(mapRes);
//ForEach() has no return value. If you are interested, you can test it yourself. }


    // reduce() merge method accepts the passed function and the initial value as the basis for merge (optional)    //The function to be passed in receives four functions, the previous value, the current value, the index item, the array object    var sum = (function(prev,cur,index,array){
       return prev + cur;
    });
    alert(sum);
    //ReduceRight() merge method is essentially the same as reduce() method, the difference is that it starts from the back to the front and inside.    var sum2 = (function(pre,cur,index,array){
       return pre + cur;
    });
    alert(sum2);
</script>
</head>

<body>
</body>
</html> 

The above five iteration methods and two merging methods of js array (recommended) are all the content I share with you. I hope you can give you a reference and I hope you can support me more.