es6 数组方法的扩展

数组的扩展

数组的类 Array
  数组的定义
    var arr = [12,23]
    var arr = new Array(3) 数字3代表 三个元素 或 3个空位
    arr[0] = ""; arr[1] = "" ; arr[2] = ""
    var arr = new Array("张","王")


    var arr = new Array("1") //代表一个数组 且值为1
    console.log(arr) //["1"]

    var arr = new Array(1) //代表n个元素
    console.log(arr) // [empty]

  数组方法:
    of方法 :
    //of
    var arr = Array.of(4)
    console.log(arr) //[4]
    返回一个元素为4的数组

    foreach 方法:
    //foreach() 遍历数组 返回值是undefined
    var arr = ["王一博","肖战","魏婴","蓝忘机"]
    arr.forEach((item,index) => {console.log(item)})


    let arr = ["王一博","肖战","魏婴","蓝忘机"]
    let as = arr.forEach((item,index) => {console.log(item)})
    console.log(as) //undefined
  
    map方法:
      //map() 遍历数组 返回值是数组
      map最大的功能将原来的数组 一一映射成一个新的数组,内部一定使用return
      let arr = ["王一博","肖战","魏婴","蓝忘机"]
      let as = arr.map((item,index) => {
        return item;
      })
      console.log(as) // ["王一博","肖战","魏婴","蓝忘机"]

      filter方法:
    //filter() 过滤 从数组中找出所有符合条件的元素,并返回成一个新的数组,不改变原来的数组
      let arr = [
        {"name":"王一博","age":22},
        {"name":"肖战","age":20},
      {"name":"谭雅宁","age":19}
      ]
      let as = arr.filter((item,index)=>{
      //return 返回的值是true 就保留 返回的值是false 就不保留
      return item.age>20;
      })
      console.log(as) // {name: "王一博", age: 22}

    find方法:
    //find() 对数组进行逐个查找,当返回值为true说明找到,直接返回结果,不再往下查找
      let arr = [
      {"name":"王一博","age":22},
      {"name":"肖战","age":20},
      {"name":"谭雅宁","age":19}
        ]
      let as = arr.find((item,index) => {
      return item.age>18;
      })
      console.log(as) //{name: "王一博", age: 22}

      every() / some()
      every():
      //every() 判断数组中的每个元素是否满足条件 ,都满足条件 返回值是true 如果有一个不满足 就是 false
        let arr = [
          {"name":"王一博","age":22},
          {"name":"肖战","age":20},
          {"name":"谭雅宁","age":19}
        ]
        let as = arr.every((item,index) => {
        return item.age>20;
          })
      console.log(as) //false

      some():
      //some 判断数组中的元素是否满足条件,只要有一个满足就返回true , 如果都不满足就是false
        let arr = [
          {"name":"王一博","age":22},
          {"name":"肖战","age":20},
          {"name":"谭雅宁","age":19}
        ]
        let as = arr.some((item,index) => {
          return item.age>21;
        })
      console.log(as) //true

    reduce():
      //reduce() 迭代 从第二个元素执行,因为第一个元素没有上一个
      //参数是一个回调函数 ,回调函数可以放三个参数 第一个prev 代表上一次reduce返回的值 第二个item是当前元素 第三个index 是当前索引
      let arr = [1,2,3,4,5,6,7,8,9,10]
      let as = arr.reduce((prev,item,index) => { //item 数组的元素从第二个开始 //prev 上一次的返回值 (第一个前面没有值)
      console.log(prev) // 1 3 6 10 15 21 28 36 45
        return prev+item;
      })
      console.log(as) //55


    //如果想让reduce从第一个元素开始执行 (默认从第二个执行) 回调函数后面的参数就是第一次执行reduce()的prev
        let arr = [1,2,3,4,5,6,7,8,9,10]
        let as = arr.reduce((prev,item) => {
          console.log(prev)
          return prev+item;
        },0) //代表从第一个元素开始,并且让他的返回值是0
      console.log(as) //55


      let name = ["tom","jim","jack","tom","jack"]
      let as = name.reduce((prev,item) => {
        if(item in prev){
          prev[item]++
        }else{
          prev[item]=1
        }  
          return prev;
        },{})
      console.log(as) // {tom: 2, jim: 1, jack: 2}

posted @ 2019-07-10 21:18  枯桑  阅读(284)  评论(0编辑  收藏  举报