JS數(shù)組方法使用教程(二)

目錄

  • array.forEach() 返回undefined
  • array.map() 返回操作后的新數(shù)組
  • array.filter() 返回符合條件的新數(shù)組
  • array.every() 條件里有一個數(shù)組元素是false,則返回false。
  • array.some() 條件里有一個數(shù)組元素是false,則返回true。除非數(shù)組里每一個是false才返回false
  • array.find() 返回找到的元素
  • array.findIndex() 返回找到元素的索引
  • array.reduce() 返回回調(diào)函數(shù)累積的結(jié)果
  • 測試題
  • 練習(xí)題

array.forEach()

定義

  • 按順序為數(shù)組中的每個元素調(diào)用一次函數(shù)

語法

array.forEach(function(item, index, arr), thisValue)
  • function:第一個參數(shù)是一個函數(shù)。必需
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
  • thisValue:第二個參數(shù)是this指向。默認值undefined,可選

返回值

  • undefined

示例

var arr = [1,2,3]
        arr.forEach(function(item, index, arr){
            console.log(1,0,arr)
            // 工作原理
            // console.log(1,0,[1,2,3])  第一圈
            // console.log(2,1,[1,2,3])  第二圈
            // console.log(3,2,[1,2,3])  第三圈
  })

et sum = 0;
        const arr = [1, 2, 3];
        arr.forEach(function(item){
        sum += item
        });
        console.log(sum) //6
const arr = [1,2,3]
        arr.forEach(function(item,index,arr){
            arr[index] = item + 3
        })
        console.log(arr) //[4,5,6]

array.map()

定義
array.map()方法使用指定函數(shù)遍歷數(shù)組。即逐一對傳入到函數(shù)體的每個數(shù)組元素進行操作,然后把每一個數(shù)組元素填充進新數(shù)組。
語法

array.map(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。

  • this: 修改函數(shù)內(nèi)的this指向,默認值undefined,可選。
    返回值
    返回操作后的新數(shù)組
    示例

const arr = [1, 2, 3];
let arrNew = [ ]
arrNew = arr.map(function(item,index){ //map()方法自動返回一個新數(shù)組
  return item + 3 // 設(shè)置function函數(shù)的返回值,返給map()方法
});
console.log(arrNew) // [4,5,6]

array.filter()

定義
array.filter()方法使用指定函數(shù)遍歷數(shù)組。即逐一對傳入到函數(shù)體的每個數(shù)組元素進行條件檢測,把符合條件的數(shù)組元素填充進新數(shù)組,跳過不符合條件的數(shù)組元素。
語法

 array.filter(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。

  • this: 修改函數(shù)內(nèi)的this指向,默認值undefined,可選。

返回值
返回符合條件的新數(shù)組
示例:篩選大于3的數(shù)組元素

const arr = [1, 2, 3, 4, 5, 6];
let arrNew = []
arrNew = arr.filter(function (item) { //filter()方法自動返回符合條件的數(shù)組元素
  return item > 3 // 設(shè)置function函數(shù)的返回值,返給filter()方法
});
console.log(arrNew) // [4,5,6]

array.every()

定義
array.every()方法使用指定函數(shù)遍歷數(shù)組。即逐一對傳入到函數(shù)體的每個數(shù)組元素進行條件檢測,符合條件就返回一個true,不符合條件就返回false。
語法

 array.every(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。

  • this: 修改函數(shù)內(nèi)的this指向,默認值undefined,可選。

返回值

  • true: 當傳入的數(shù)組元素全部返回true時,返回true。
  • false: 當傳入的數(shù)組元素有一個返回false,則返回false。

示例

var arr = [1,2,3]
var result = arr.every(function(item){
  return item > 0
})
console.log(result) // true

array.some()

定義
array.some()方法使用指定函數(shù)遍歷數(shù)組。即逐一對傳入到函數(shù)體的數(shù)組元素進行條件檢測,符合條件就返回一個true并停止檢測,不符合條件就返回false。
語法

array.some(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。
  • this: 修改函數(shù)內(nèi)的this指向,默認值undefined,可選。

返回值

  • true: 當傳入的數(shù)組元素有一個返回true時,返回true。
  • false: 當傳入的數(shù)組元素全部返回false,則返回false。

示例

var arr = [1,2,3]
var result = arr.some(function(item){
  return item > 2
})
console.log(result) // true

array.find()

定義
array.find()方法使用指定函數(shù)遍歷數(shù)組。即逐一對傳入到函數(shù)體的數(shù)組元素進行條件查找,找到符合條件的數(shù)組元素則返回true,然后返回該元素。
語法

 array.find(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。
  • this: 修改函數(shù)內(nèi)的this指向,默認值undefined,可選。

返回值
返回找到的元素。
示例

var arr = [1,2,3]
var result = arr.find(function(item){
  return item == 2
})
console.log(result) // 2

array.findIndex()

定義
array.findIndex()方法使用指定函數(shù)遍歷數(shù)組。即逐一對傳入到函數(shù)體的數(shù)組元素進行條件查找,找到符合條件的數(shù)組元素則返回true,然后返回該元素的索引。
語法

 array.findIndex(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。
  • this: 修改函數(shù)內(nèi)的this指向,默認值undefined,可選。

返回值
返回找到元素的索引。
示例

var arr = [1,2,3]
var result = arr.findIndex(function(item){
  return item == 2
})
console.log(result) // 1

array.reduce()

定義
array.findIndex()方法為數(shù)組的每個值(從左到右)執(zhí)行提供的函數(shù)。該方法將數(shù)組縮減為單個值。
語法

array.reduce(function(item, index, arr), this)
  • function: 遍歷數(shù)組的函數(shù)。必須。
    --total: 用于求和的變量。
    --item: 接收數(shù)組元素的形參,必須。
    --index: 接收數(shù)組索引的形參,可選。
    --arr: 接收當前數(shù)組的形參,可選。
  • initialValue: 初始值。

返回值
返回回調(diào)函數(shù)累積的結(jié)果。
示例

var arr = [1, 2, 3]
var result = arr.reduce(function (total, item) {
    return total += item
}, 0)
console.log(result) // 6
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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