数组迭代方法
-
every:针对数组做些判断,如果结果都为ture,最后才为true
//1.every:针对数组做些判断,如果结果都为ture,最后才为true var arr = [89,98,96,100,88]; //如果所有的成绩都在80以上,拉出去打一顿 var n = 0; var isD = arr.every(function(value,index,array){ //value:数组项,index下标 array:当前操作的数组 return value>80; }); console.log(isD); //false
-
some():针对数组做些判断,只要有一个为ture,最后就为true
//2.some:针对数组做些判断,只要有一个为ture,最后就为true var arr = [59,68,36,100,18]; //只要有一门成绩100+,拉出去打一顿,偏科 var is = arr.some(function(value){ return value >= 100; }); if(is ==true){ console.log("拉出去打一顿,偏科"); }
-
filter:针对数组做些判断,满足条件的会组成一个新的数组返回
//3.filter:针对数组做些判断,满足条件的会组成一个新的数组返回 var arr = [ {"name":"手机","type":"华为meta40","price":8999}, {"name":"电脑","type":"mac","price":13999}, {"name":"电脑","type":"外星人","price":139999}, {"name":"手机","type":"诺基亚","price":99}, {"name":"电视","type":"小米","price":999} ]; var newArr = arr.filter(function(value){ return value.name == "电视"; }); console.log(newArr);
-
map():返回的值会组成一个新的数组
//4.map:返回的值会组成一个新的数组 var arr = [56,78,54,98,12]; var arr = arr.map(function(value,index){ console.log(index+"-------"+value); return value+5; }); console.log(arr); // [61, 83, 59, 103, 17]
-
forEach():循环数组
//5.forEach arr.forEach(function(value,index){ console.log(index+"--------"+value); })