數(shù)組方法map,reduce,fitter,forEach

  • map 映射
let arr=[1,2,3]
arr.map(item=>item*2)

  • reduce 匯總
 let arr1 = [1, 0, 4, 0, 10];
    arr1 = arr1.reduce((temp, item, index) => {
        return temp + item;
      });
      console.log(arr1);
中間結(jié)果.png

中間結(jié)果
/* 中間結(jié)果是temp ,index是下標(biāo)
第一次temp是1 = arr1[0],item=0,index就是1
第二次temp是1 ,item=4, index就是2
/
/
當(dāng)前的零食結(jié)果 temp + 加上這一次的 item 就是 中間結(jié)果
*/

image.png


  • filter 過濾器
 let arr2 = [1, 4, 15, 16, 10];
      arr2 = arr2.filter(item => item % 2 == 0);
      console.log(arr2);
let goods = [
  { title: "男士包", price: 10 },
  { title: "女士包", price: 10000 },
  { title: "男士襯衫", price: 100 },
  { title: "女士鞋", price: 20000 }
];
let result = goods.filter(item => item.price >= 10000);
console.log(result);
  • forEach 迭代
   let arr5 = [1, 23, 4, 56, 9];
      arr5.forEach((item, index) => {
        console.log(item + " " + index);
      });
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容