js 数组array es5-es6+ 新增方法函数

arr.forEach(function(item,index,arr){},this)  相当于普通的for循环,第一个回调参数,第二个this可以重定向【箭头函数则不生效】

arr.map() 非常有用,做数据交互 配合return,返回一个新数组,没有return则相当于forEach(每一项则是undefined)

 

注意:平时只要用map,要有return

列如 
  [{title:"aaa"}]-->[{label:"aaa"}]   

  arr.map((item)=>{

    let {title:label}=item;

  return {label};

  })

 

arr.filter()  返回过滤所有满足条件的新数组 

列如:[{score:95,id:"13"},{score:55,id:"14"},{score:35,id:"15"},{score:65,id:"16"}] 过滤数组score中大于59以上的
arr.filter(item=>item.score>59)  => [{score:95,id:"13"},{score:65,id:"16"}] 

 

posted @ 2019-06-11 14:44  KingX  阅读(552)  评论(0编辑  收藏  举报