迭代方法

2018-03-07  本文已影响0人  YoungEvita
方法 参数 返回值
array.every(function(item, index, arr), thisValue) item(必需) - 当前元素值;
index(可选) - 当前元素索引;arr(可选) - 当前元素所属数组;
thisValue(可选) - 对象作为该执行回调时使用,传递函数,用在this的值。若省略,默认undefined
数组中所有元素满足条件时返回true,有一个元素不满足,返回false,且停止遍历。
array.some(function(item, index, arr), thisValue) 同上 数组中有一个元素满足条件时返回true,且停止遍历;所有元素不满足条件,返回false
array.filter(function(item, index, arr), thisValue) 同上 返回满足条件的所有元素,若没有满足条件的元素,返回空数组。
array.find(function(item, index, arr), thisValue) 同上 返回满足条件的第一个元素,找到之后即刻停止遍历,若找不到返回-1
array.map(function(item, index, arr), thisValue) 同上 返回每次函数调用结果的组成的数组
array.forEach(function(item, index, arr), thisValue) 同上 对数组中的每一项运行给的函数,无返回值

以上方法对空数组不做检测;
都不改变原有数组;
不支持break跳出遍历,也不支持return false跳出遍历。

方法 返回值
every true
some false
filter [] (空数组)
find undefined
map [] (空数组)
forEach undefined

例子

// every 方法
var arr = [1, 2, 3, 4, 5];
var everyRes = arr.every(function(item, index, arr) {
    console.log(item); // 1
    return item > 2;
});
console.log(everyRes); // false

var everyEmpty = [].every(function(item, index, arr) {
    console.log(item); // 不会执行
    return item > 2;
})
console.log(everyEmpty); // true 

// some 方法
var arr = [1, 2, 3, 4, 5];
var someRes = arr.some(function(item, index, arr) {
    console.log(item); // 1 2 3
    return item > 2;
});
console.log(someRes); // true

var someEmpty = [].some(function(item, index, arr){
    console.log(item); // 不会执行
    return item >2;
});
console.log(someEmpty); // false
// filter 方法
var arr = [1, 2, 3, 4, 5];
var filterRes = arr.filter(function(item, index, arr) {
    console.log(item); // 1 2 3 4 5
    return item > 2;
});
console.log(filterRes); // [3, 4, 5]

var filterEmpty = [].filter(function(item, index, arr) {
    console.log(item); // 不会执行
    return item > 2;
});
console.log(filterEmpty); // []
// find 方法
var arr = [1, 2, 3, 4, 5];
var findRes = arr.find(function(item, index, arr) {
    console.log(item); // 1 2 3
    return item > 2;
});
console.log(findRes); // 3

var findEmpty = [].find(function(item, index, arr) {
    console.log(item); // 不会执行
    return item > 2;
})
console.log(findEmpty); // undefined
// map 方法
var arr = [1, 2, 3, 4, 5];
var mapRes = arr.map(function(item, index, arr) {
    console.log(item); // 1 2 3 4 5
    return item * 2;
});
console.log(mapRes); // [2, 4, 6, 8, 10]

var mapEmpty = [].map(function(item, index, arr) {
    console.log(item); // 不执行
    return item * 2;
});
console.log(mapEmpty); // []
// forEach 方法
var arr = [1, 2, 3, 4, 5];
var forEachRes = arr.forEach(function(item, index, arr) {
    console.log(item); // 1 2 3 4 5
    return item * 2;
});
console.log(forEachRes); // undefined

var forEachEmpty = [].forEach(function(item, index, arr) {
    console.log(item); // 不执行
    return item * 2;
});
console.log(forEachEmpty); // undefined

小试牛刀

1. 下面程序执行完之后result的值是多少?
var arr = [1, 2, 3];
var result = arr.forEach(function(item) {
  return item * 2;
});
A. [];
B. [0, 2, 4]
C. [2, 4, 6]
D. undefined
2. 下面程序执行之后result的值是多少?
var arr = [1, 2, 3];
var result = arr.map(parseInt);
A. ["1", "2", "3"]
B. [1, 2, 3]
C. [0, 1, 2]
D. other
上一篇 下一篇

猜你喜欢

热点阅读