ES6--5.数组4个新增方法

1.map  映射

例子:

 let arr = [2, 5, 8];

 let result = arr.map(item => item * 2);

 alert(result);//4,10,16

例子:

 let score=[19, 85, 99, 25, 90];

 let result=score.map(item=>item>=60?'ok':'no');

 alert(result);

 

2.reduce 汇总

例子:

//求和
let arr=[12,13,14,15]; let result=arr.reduce((tem,item,index)=>tem+item); alert(result);

 

3.filter 过滤

例子:

let arr=[12,5,8,99,27,36,75,11];

let result=arr.filter(item=>item%5==0);

alert(result);

例子2

  let arr=[
      {title: '男士衬衫', price: 75},
      {title: '女士包', price: 842},
      {title: '男士包', price: 65},
      {title: '女士鞋', price: 531}
    ];

    let result=arr.filter(json=>json.price>=100);

 

4.forEach 循环 

例子:

 let arr=[12,5,8,9];
    arr.forEach((item,index)=>{
      console.log(index,item);
    });

 

posted @ 2017-12-13 15:36  魔都叛徒  阅读(143)  评论(0编辑  收藏  举报