1.Array.forEach() 遍历数组 无需return
var animals = [‘dog‘, ‘cat‘, ‘mouse‘];
animals.forEach(function(item){
console.log(item);
});
2.Array.map() 遍历数组 并生成新的数组
var numbers = [2, 4, 6, 8];
var doubleNums = numbers.map(function(element) {
return element * 2;
});
console.log(‘doubleNums: ‘, doubleNums)
3.Array.filter() 筛选数组成员
var scores = [3, 12, 5, 23, 19, 7];
var topScores = scores.filter(function(item){
if (item > 10){
return true;
} else {
return false;
}
});
console.log(‘topScores: ‘, topScores);
4.Array.indexOf() 获取数组中某个元素的索引值(不存在 值未 -1)
var a = [2, 9, 9, 18];
var i = a.indexOf(9);
console.log(‘i: ‘, i);
5.Array.every() 匹配数组中的每个元素 是否符合条件
var ages = [23, 19, 32, 44];
var olderThan18 = ages.every(function(element) {
return element > 18;
});
console.log(‘olderThan18: ‘, olderThan18);