javascript——数组的使用方法详解(让你使用数组更加快乐)

javascript 数组的方法
导读(顺序以打乱)

  • array.forEach()
  • array.map()
  • array.filter()
  • array.every()
  • array.findIndex()
  • array.find()
  • array.some()
  • array.reduceRight()
    -array.reduce()

array.forEach

定义
array.forEach()方法使用指定函数遍历数组。即逐一传入数组元素到函数体内,对函数体内的数组元素进行何种操作,由开发者决定
语法

    array.forEach(function(item, index, arr), this)
  • function: 遍历数组的函数。必须。
  • item: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选
  • this: 修改函数内的this指向,默认值undefined,可选。
    返回值
    返回undefined
    示例
const arr = [1, 2, 3];
arr.forEach(function(item,index){
  arr[index] = item + 3
});
console.log(arr) // [4,5,6]

示例元素之和

let sum = 0;
const arr = [1, 2, 3];
arr.forEach(function(item){
  sum += item
});
console.log(sum) //10

array.map()

定义
使用为每个数组元素调用函数的结果创建新数组。
map方法不改变原数组 但是他会把原方法返回值存储到新数组中
语法

array.map(function(currentValue, index, arr), thisValue)

示例

var arr2 = []
       arr2 = arr.map(function(x){
        return x + 3
       })
       console.log(arr2)

reduceRight

定义:reduceRight() 方法的功能和 reduce() 功能是一样的,不同的是 reduceRight() 从数组的末尾向前将数组中的数组项做累加。
语法:

array.reduceRight(function(total, currentValue, currentIndex, arr), initialValue)

参数:

  • total 必需。初始值, 或者计算结束后的返回值。
  • currentValue 必需。当前元素
  • currentIndex 可选。当前元素的索引
  • arr 可选。当前元素所属的数组对象。
  • initialValue 可选。传递给函数的初始值

返回值:
注意:reduce() 对于空数组是不会执行回调函数的
示例:从右到左,减去每个数组元素:

let arr = [1,2,3,4,5]
    let sum = arr.reduceRight(function(v,item,index,arr){
    return v - item
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ]-5

Some

定义:检查数组中的任何元素是否通过特定条件
语法:

array.some(function(currentValue, index, arr), thisValue)

参数:

第一个参数:function(currentValue, index, arr) 必需。为数组中的每个元素运行的函数。

函数参数:
sum:用于存储数组之和的变量。
currentValue 必需。当前元素。
index 可选。当前元素的数组索引。
arr 可选。当前元素所属的数组对象

第二个参数:thisValue

可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,则值 "undefined" 将作为其 "this" 值传递
返回值:布尔值。如果数组中的任何元素通过测试,则返回 true,否则返回 false。
注意:
示例:检测数组中有没有比5大的数组元素

 let arr = [1,2,3,4,5]
    let sum = arr.some(function(item){
    return item > 5
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] false

reduce

定义:接收一个函数作为累加器,数组中的每个值(从左到右)开始增减,最终计算为一个值。
语法:

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)

参数:

  • total 必需。初始值, 或者计算结束后的返回值。
  • currentValue 必需。当前元素
  • currentIndex 可选。当前元素的索引
  • arr 可选。当前元素所属的数组对象。
  • initialValue 可选。传递给函数的初始值

返回值:返回计算结果
注意: reduce() 对于空数组是不会执行回调函数的。
示例:数组的每一个元素进行相加

 let arr = [1,2,3,4,5]
    let sum = arr.reduce(function(v,item,index,arr){
    return v + item
    })
    console.log(arr,sum)// [1,2,3,4,5] 15

Every

定义:检验数组中的所有元素是否都通过了设定的条件(被作为函数提供)。every() 方法对数组中存在的每个元素执行一次函数。
语法:

array.every(function(数组元素, index, arr), thisValue)

参数:

  • Function(){} 必选
    调用函数的时候,可以在函数内传入一下三个参数
    item: 数组中的一个元素元素,必须。
    index: 数组索引,可选。
    arr: 当前数组,可选。
  • this :可选

返回值:*布尔值。如果数组中的所有元素都通过设定的条件,则返回 true,否则返回 false.
示例:检测数组元素是否都等于0

  let arr = [1,2,3,4,5]
    arr.every(function(tiem){
        console.log(tiem > 0)//true
    })

Findindex

定义:返回数组中通过条件的第一个元素的索引
语法:

array.findIndex(function(currentValue, index, arr), thisValue)

参数:

第一个参数:function(currentValue, index, arr) 必需。为数组中的每个元素运行的函数。

函数参数:
currentValue 必需。当前元素。
index 可选。当前元素的数组索引。
arr 可选。当前元素所属的数组对象

第二个参数:thisValue

可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,就是undefined。

返回值:
如果数组中的任何元素通过条件,则返回数组元素索引,否则返回 -1。

注意:
示例:获取数组中第一个值大于 3 的元素的索引

 let arr = [1,2,3,4,5]
    let sum = arr.find(function(item){
    return item > 3
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] 3

Find

定义:返回数组中第一个通过测试的元素的值
语法:

array.find(function(currentValue, index, arr), thisValue)

参数:

第一个参数:function(currentValue, index, arr) 必需。为数组中的每个元素运行的函数。

函数参数:
currentValue 必需。当前元素。
index 可选。当前元素的数组索引。
arr 可选。当前元素所属的数组对象

第二个参数:thisValue

可选。要传递给函数以用作其 "this" 值的值。
如果此参数为空,就是undefined。
返回值:如果数组中的任何元素通过测试,则返回数组元素值,否则返回 undefined。
注意:
示例:获取到比数组元素3大的数组元素

 let arr = [1,2,3,4,5]
    let sum = arr.find(function(item){
    return item > 3
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ] 4

Filter

定义:用于筛选数组元素,返回符合条件的新数组,略过不符合的元素,不修改原数组
语法:

array.filter(function(item index, arr), thisValue)

参数:

  • Function(){} 必选
    调用函数的时候,可以在函数内传入一下三个参数
    item: 数组中的一个元素元素,必须。
    index: 数组索引,可选。
    arr: 当前数组,可选。
  • this :可选

返回值:包含所有通过测试的数组元素的数组。如果没有元素通过测试,则返回一个空数组。
注意:
示例:筛选出大于或等于3的数组元素

 let arr = [1,2,3,4,5]
    let sum = arr.filter(function(item,index,arr){
    return item >= 3
    })
    console.log(arr,sum)//[ 1, 2, 3, 4, 5 ][ 3, 4, 5 ]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容