let arr = [2,3,6,8,1,5];
1.1、for循环
for(var i = 0 ; i < arr.length ; i++){
console.log(arr[i]);
}
1.2、for....in....
for(var index in arr){
console.log('索引: '+index,"值: "+arr[index]);
}
1.3、forEach()
arr.forEach( (item,index) => {
console.log('索引:'+index,'数据:'+item);
})
1.4、map()
let resArr = arr.map( (item,index) => {
return item*2
})
console.log(resArr)
1.5、filter()
let resArr = arr.filter( item => item >= 5 )
console.log(resArr)
1.6、some()
筛选数组中是否有符合条件的数,返回值是boolean布尔值
let isHas = arr.some( item => item == 8)
console.log(isHas);
1.7、every()
let isAllHas = arr.every( item => item > 5)
console.log(isAllHas);
1.8、reduce()求和
let result = arr.reduce( (pre,cur,index,arr) => {
return pre+cur;
},0)
console.log(result);
1.9、indexOf()
判断数组中是否有该元素,不存在则返回-1
let result = arr.indexOf(4);
console.log(result);
1.10、ES7中的includes()
因为indexOf无法检测NaN,因此在ES7中弥补了该部分
let arr = [2, 3, 6, 8, 1, 5,NaN,'ref'];
let a = arr.includes(2);
let b = arr.includes(NaN);
let c = arr.includes('ref');
console.log('a的结果:'+a,'b的结果:'+b,'c的结果:'+c);