• map()数组遍历


    map() 方法对数组的每个元素执行一次给定的函数。只对数组有效

    特性

    map()返回新数组

    语法:

    arr.map(callback(currentValue [, index [, array]])[, thisArg])

    参数:

    arr.map有三个参数,分别是:

    1、arr:被遍历的数组

    2、callback(currentValue,index,array){句柄}:回调函数,该回调函数接受三个参数:

      A、currentValue:遍历到的当前元素

      B、index:为currentValue的索引

      C、array:被遍历的数组

    3、thisArg:指代遍历中this的值

    示例:

        let arr = [1, 2,3]
        let newArr=arr.map(function (currentValue, index, ar) {
            console.log(currentValue);//遍历打印1,2,3
            console.log(index);//遍历打印0,1,2
            console.log(ar);//遍历打印三次[1, 2, 3]
            console.log(this)//String {"我就是this的值"};遍历打印三次
            return ar[index]*2
          }, "我就是this的值")
          //输出
          console.log(arr);//[1, 2,3]           未改变源数组
          console.log(newArr);//[2, 4, 6]       返回新数组

     map()和forEach()的区别:

    1、map()会返回新的数组,而forEach不会返回有意义的值

    2、运行速度:map()>forEach()

    3、是否改变源数组:map()不改变;forEach()改变

  • 相关阅读:
    min-width和width的区别
    组装电脑
    css背景透明,文字不透明
    三十九、前端基础之HTML
    三十八、事务、mysql索引,视图
    三十七、python操作mysql,和navicat
    三十六、单表与多表查询
    三十五、表与表之间的关系
    三十四、字段类型
    三十三、初识数据库及简单命令
  • 原文地址:https://www.cnblogs.com/vinson-blog/p/13021953.html
Copyright © 2020-2023  润新知