日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ES6数组新增方法

發布時間:2023/12/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES6数组新增方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ES6數組新增的一些常用的方法

  • forEach
  • map
  • filter
  • some
  • every
  • find
  • findIndex
  • findLast
  • findLastIndex
  • reduce

以上這些方法,用法都一樣,效果不同

arr.方法名((item,index,arr)=>{ })

1. forEach

此方法是用來代替 for 循環遍歷數組

let arr=[1,2,3,4]; arr.forEach(function(value,index,arr){ //在這里進行相關操作 })

2.map

返回值是一個新的 數組,數組長度和原數組相同,數組中的值,就是函數中的返回值。

let potatos = [{ id: '1001', weight: 50 },{ id: '1002', weight: 80 },{ id: '1003', weight: 120 },{ id: '1004', weight: 40 },{ id: '1005', weight: 110 },{ id: '1006', weight: 60 } ]let w = potatos.map(function(potato) {return potato.weight})//在這里,potato.weight就是函數的返回值

3.filter

此方法是依次拿出數組中的元素,返回符合要求的元素。返回值是一個新的數組,數組中的值是符合條件的值,而這個條件是函數的返回值。

let potatos = [{ id: '1001', weight: 50 },{ id: '1002', weight: 80 },{ id: '1003', weight: 120 },{ id: '1004', weight: 40 },{ id: '1005', weight: 110 },{ id: '1006', weight: 60 } ] let bigPotatos=potatos.filter(potato=>potato.weight>=100) //potato.weight>=100 就是返回值,為布爾值,如果為true,則當前遍歷到potato就會作為新數組中的值

4.some

此方法返回值是布爾值,判斷數組中是否有符合條件的值,而這個條件是函數的返回值

let potatos = [{ id: '1001', weight: 50 },{ id: '1002', weight: 80 },{ id: '1003', weight: 120 },{ id: '1004', weight: 40 },{ id: '1005', weight: 110 },{ id: '1006', weight: 60 } ]let hasBig = potatos.some(potato => potato.weight >= 100) // 只要返回值為true,則內部停止遍歷,some返回值true,如果每次都返回false,則some返回值為false

5.every

返回值是布爾值,判斷數組中的值是否都符合條件,如果是則返回true,有一個不符合則返回false

let potatos = [{ id: '1001', weight: 50 },{ id: '1002', weight: 80 },{ id: '1003', weight: 120 },{ id: '1004', weight: 40 },{ id: '1005', weight: 110 },{ id: '1006', weight: 60 } ]let hasBig = potatos.every(potato => potato.weight >= 100) // 只要所有返回值為true,則every返回true,如果由一次返回false,則every返回值為false

6.find 、findLast

返回值為符合條件的對應的那個值
后者從后往前遍歷

let potatos = [{ id: '1001', weight: 50 },{ id: '1002', weight: 80 },{ id: '1003', weight: 120 },{ id: '1004', weight: 40 },{ id: '1005', weight: 110 },{ id: '1006', weight: 60 } ]let bigPotato = potatos.find(potato => potato.weight >= 100) // 得到第一個符合條件的數據,返回給變量

7.findIndex 、findLastIndex

返回值為符合條件的對應的那個值的下標
后者從后往前遍歷

let potatos = [{ id: '1001', weight: 50 },{ id: '1002', weight: 80 },{ id: '1003', weight: 120 },{ id: '1004', weight: 40 },{ id: '1005', weight: 110 },{ id: '1006', weight: 60 } ]let bigPotato = potatos.findIndex(potato => potato.weight >= 100) // 得到第一個符合條件的數據的下標,返回給變量

總結

以上是生活随笔為你收集整理的ES6数组新增方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。