zoukankan      html  css  js  c++  java
  • foreach和map遍历

    参考网址:http://www.cnblogs.com/jocyci/p/5508279.html

    1.foreach :

    • 理论上这个方法是没有返回值的,仅仅是遍历数组中的每一项,不对原来数组进行修改;但是我们可以自己通过数组的索引来修改原来的数组

    var ary = [12,23,24,42,1];
    var res = ary.forEach(function (item,index,input) {
    input[index] = item*10;
    })
    console.log(res);//-->undefined;
    console.log(ary);//-->会对原来的数组产生改变;

    2.map:

    map的回调函数中支持return返回值;return的是啥,相当于把数组中的这一项变为啥(并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了

    var ary = [12,23,24,42,1];
    var res = ary.map(function (item,index,input) {
    return item*10;
    })
    console.log(res);//-->[120,230,240,420,10];
    console.log(ary);//-->[12,23,24,42,1];
    VM239:5 [120, 230, 240, 420, 10]
    VM239:6 [12, 23, 24, 42, 1]

  • 相关阅读:
    [NOI2014]动物园
    2018.7.15模拟赛
    2018.7.13模拟赛
    [CodeForces]920F SUM and REPLACE
    [BZOJ3211]花神游历各国
    [GSS5] Can you answer these queries V
    [SPOJ1716] GSS3
    [HNOI2012]排队
    2018.7.10模拟赛
    7.3模拟赛
  • 原文地址:https://www.cnblogs.com/maochunyan/p/7513861.html
Copyright © 2011-2022 走看看