Five iteration methods and two merge methods for js arrays recommended by of
- 2021-06-28 10:40:10
- OfStack
Five iteration methods and two merge methods for js array (recommended)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title> Untitled Document </title>
<script>
window.onload = function(){
//every() Equivalent to Logical AND
var arr = [1,2,3,4,5,6,7,8];
var everyRes = arr.every(function(item,index,array){
return (item>2);
});
alert(everyRes);
//some() Equivalent to logical or
var someRes = arr.some(function(item,index,array){
return (item>2);
});
alert(someRes);
//filter() Returns an array of given conditions
var filterRes = arr.filter(function(item,index,array){
return (item>2);
});
alert(filterRes);
//map() Returns an array of given conditions
var mapRes = arr.map(function(item,index,array){
return (item*2);
});
alert(mapRes);
//forEach() no return value Interested in self-testing
}
//reduce() Merge method Accept incoming functions and initial values as merge basis ( Optional
// Function Receive To Incoming 4 Functions, first 1 Values, Current Value, Index Item, Array Object
var sum = arr.reduce(function(prev,cur,index,array){
return prev + cur;
});
alert(sum);
//reduceRight() Merge method and reduce() Method Essence 1 So, the difference is that you start from the back and start from the front.
var sum2 = arr.reduceRight(function(pre,cur,index,array){
return pre + cur;
});
alert(sum2);
</script>
</head>
<body>
</body>
</html>