循环方法.png
    (不影响原数组)forEach:对数组中的每一项运行给定函数,这个方法没有返回值.
    参数为function类型,默认有传参(遍历数组内容,对应数组索引,数组本身)
    在这里引用一个segmentfault的回答:https://segmentfault.com/q/1010000006127658?_ea=1022444,以下为引用的内容:
    题目之添加一个 fullName。
    所传函数有三: forEach(function (子项,Index,所有项All) {})
    1)不用 forEach
    var users = [
    {
    lastName: ‘Li’,
    firstName: ‘Lei’
    }, {
    lastName: ‘Han’,
    firstName: ‘Meimei’
    } ];
    // 在这里,我们用原始的语法处理
    for (var i = 0; i < users.length; i++) {
    users[i].fullName = users[i].firstName + users[i].lastName;
    }
    console.log(users);
    o
    分析缺点:我们多了个 i 变量,直观上多了个 for 循环
    o
    若是用forEach
    2)使用 forEach
    users.forEach(function(user,index,arr){
    user.fullName = user.firstName + user.lastName;
    });
    console.log(users);
    你可能认为这里的 forEach 其实改变了数组的值,其实不然。
    只需知道,引用对象是不同的,他们指向的是一个内存位置,而非实际的对象。
    var arr = [1, 2, 3];
    arr.forEach(item => {
    item = 8;
    });
    console.log(arr); // [1, 2, 3]


    (不影响原数组)map:对数组中的每一项运行给定函数,返回每次调用结果组成的数组。
    我们能够在 Redux 中的 reducer 看到大量的 map 函数,因为 redux 追求这种函数式极简的酷炫的风格; Vue 中的 v-for 本质上就是 map 实现的; map是指映射,和英文中的地图不同。
    所传函数有三: forEach(function (子项,Index,所有项All) {})
    1)map(function (item, index, allItem) {})
    var a = [1, 32, 442, 234];
    b = a.map(item => item + 1);
    console.log(a, b); // [1,32, 442, 234] [2, 33, 443, 235]
    2) 如果我们什么都返回,则每一项都会返回 undefined
    var a = [1, 32, 442, 234];
    c = a.map(() => {});
    console.log(c); // [ undefined, undefined, undefined, undefined ]
    3) 回调函数中第二个参数 index 的作用,在react中我们主要将它赋值给key
    d = a.map((item, i) => {
    console.log(i); // 0,1,2,3
    });
    4) 回调函数中第三个参数,所有元素,即数组本身
    e = a.map((item, i, array) => {
    console.log(array); // 最后会打印四遍 [1,34,442,234]
    })


    (不影响原数组)filter:对数组中的每一项运行给定函数,返回该函数会返回 true 的项组成的数组
    以上不改变原数组的函数如果结果需要多次使用,最好都定义一个新的变量来存储。
    让我看一个简短的filter,是不是有一种四两拨千斤的感觉。
    var a = [{
    flag: false,
    name: ‘lihua’
    }, {
    flag: true,
    name: ‘zengcuo’
    }];
    console.log(a.filter(item => item.flag)); // [{flag:true,name:’zengcuo’}]
    除此之外,ES5 新增了两个归并的方法
    1) (不影响数组)reduce(function(pre, next, index, array) {}) 从左到右
    2) (不影响数组)reduceRight(function(pre, next, index, array) {}) 从右到左
    let arr = [1, 2, 3];
    console.log(arr.reduce((pre, next) => pre + next)); // 6


    (不影响原数组)some和every
    1) some: 对数组中的每一项运行给定函数,如果函数对任一项返回 true,就会返回 true,否则 false
    2) every:对数组中的每一项运行给定函数,如果函数对所有的项都返回 true,才返回 true,否则 false
    var people = [‘crimeA’, ‘crimeB’];
    console.log(people.some(item => item.indexOf(‘crimeA’) > -1)); // true 只要有罪行A 就返回 true
    console.log(people.every(item => item.indexOf(‘crimeA’)> -1)); // false 每个数组都要 罪行A 才返回 true