1.forEach(Es5) (遍历数组)
let arr =[10,11,12,13,14];
arr.forEach(function (item,index){
console.log(index);
});
2.map 与forEach用法一致 对数组中每一个元素执行相同的操作,返回一个新的数组
let arr =[10,11,12,13,14];
let newArr = arr.map(function(item , index){
return item * 2;
});
console.log(newArr);
3. let arr =[10,11,12,13,14]; 过滤,筛选
let newArr = arr.filter(function (item,index){
return item %2 ===0;
});
console.log(newArr);
let arr =[10,11,12,13,14,11,12]; 去重
let newArr = arr.filter(function (item,index){
return arr.indexOf(item)===index;
});
console.log(newArr);
4.reduce
let arr =[10,11,12,13,14]; 比大小
let result = arr.reduce(function(a,b){
console.log(a,b);
return a>b?a:b;
});
console.log(result);
5.every
let arr = [10, 11, 12, 13, 14]; 对每一个元素进行判断,一假全假
let result = arr.every(function (item, index) {
console.log(item,index);
return item > 3;
})
console.log(result);
6.some
let arr = [10, 11, 12, 13, 14]; 对每一个元素进行判断,一真全真
let result = arr.some(function (item, index) {
console.log(item,index);
return item >14 ;
});
console.log(result)