javascript--数组方法使用详解(二)

除了forEach,其他用返回值return

array.forEach

定义:指定函数遍历数组

  • 官方:按顺序为每个数组中的每个元素调用一次函数
  • 理解:使用指定函数遍历数组
    语法
array.forEach(function(item, index, arr), this)
  • array:数组
  • for:为了。。。。谁
  • Each:每一个元素
  • function: 第一个参数是一个函数,必须.

在调用函数时,可以传入以下三个参数:

  • ltem:表示数组中的一个元素,必须
  • index:数组索引,可选
  • arr: 当前数组,可选
  • this: 指代词,修改函数内的this指向(调用谁它指任的就是调用的)
    默认this没有指向返回Window(全局)
    默认值undefined,可选。

返回值:forEach返回undefined

this示例1

function fn(){
 console.log(this)
   }
    // fn()//默认没有指向this返回Window(全局)

this示例2

var arr = []
 arr.fn = function(){
console.log(this)
}
  // arr.fn()//Array []数组调用了数组

this示例3

 String.b1= function(){
        console.log(this)
    }
    String.b1()
//function String()

forEach示例1

 var str =[1,2,3]
    arr.forEach(function(item,index,arr){
        console.log(1,0,[1,2,3])
      //  console.log(2,1,[1,2,3])
       // console.log(3,2,[1,2,3])
    })
打印结果
    console.log(str) 1,2,3

forEach示例2**输出[4,5,6]

arr.forEach(function(item,index){
  arr[index] = item + 3
});
console.log(arr) // [4,5,6]

forEach输出数组元素之和示例3

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

array.map()

定义:使用指定函数遍历数组。
即逐一对传入到函数体的每个数组元素进行操作,然后把每一个数组元素填充进新数组

语法

 array.map(function(item, index, arr), this)

  • function: 遍历数组的函数。必须。
  • item: 接收数组元素的形参,必须。
  • index: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选。
  • this: 修改函数内的this指向,默认值undefined,可选。

返回值:返回操作后的新数组

示例map输出[4,5,6]

const arr = [1, 2, 3];
let arrNew = [ ]
arrNew = arr.map(function(item,index){ //map()方法自动返回一个新数组
  return item + 3 // 设置function函数的返回值,返给map()方法
});
console.log(arrNew) // [4,5,6]

array.filter()

定义:指定函数遍历数组。
即逐一对传入到函数体的每个数组元素进行条件检测,把符合条件的数组元素填充进新数组,跳过不符合条件的数组元素。(条件由开发者设计)
语法

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

返回值:返回符合条件的新的数组

示例1打印大于3

const arr = [1, 2, 3, 4, 5, 6];
let arrNew = []
arrNew = arr.filter(function (item) { //filter()方法自动返回符合条件的数组元素
  return item > 3 // 设置function函数的返回值,返给filter()方法
});
console.log(arrNew) // [4,5,6]

array.every()

定义:对数组进行检查元素.
即逐一对传入到函数体的每个数组元素进行条件检测,符合条件就返回一个true,不符合条件就返回false。(条件由开发者设计)

语法

 array.every(function(item, index, arr), this)
  • every单词:每个,每次,每一
  • function: 遍历数组的函数。必须。
  • item: 接收数组元素的形参,必须。
  • index: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选。
  • this: 修改函数内的this指向,默认值undefined,可选。

返回

  • true: 当传入的数组元素全部返回true时,返回true。
  • false: 当传入的数组元素有一个返回false,则返回false。

示例

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

array.some()

定义:指定函数遍历数组。
即逐一对传入到函数体的数组元素进行条件检测,符合条件就返回一个true并停止检测,不符合条件就返回false。(条件由开发者设计)

语法

 array.some(function(item, index, arr), this)
  • some:有一些(的意思)
  • function: 遍历数组的函数。必须。
  • item: 接收数组元素的形参,必须。
  • index: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选。
  • this: 修改函数内的this指向,默认值undefined,可选。

返回值

  • true: 当传入的数组元素有一个返回true时,返回true。
  • false: 当传入的数组元素全部返回false,则返回false。

示例

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

array.find()

定义:指函数遍历数组。
即逐一对传入到函数体的数组元素进行条件查找,找到符合条件的数组元素则返回true,然后返回该元素。(条件由开发者设计)
语法

array.find(function(item, index, arr), this)
  • find:找到发现
  • function: 遍历数组的函数。必须。
  • item: 接收数组元素的形参,必须。
  • index: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选。
  • this: 修改函数内的this指向,默认值undefined,可选。

返回值:返回找到的元素。

示例

var result = arr.find(function(item){
  return item == 2
})
console.log(result) // 2

array.findIndex()

定义:指定函数遍历数组。
即逐一对传入到函数体的数组元素进行条件查找,找到符合条件的数组元素则返回true,然后返回该元素的索引。(条件由开发者设计)

语法

 array.findIndex(function(item, index, arr), this)
  • findIndex查找索引
  • function: 遍历数组的函数。必须。
  • item: 接收数组元素的形参,必须。
  • index: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选。
  • this: 修改函数内的this指向,默认值undefined,可选

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

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

array.reduce()

定义:为数组的每个值(从左到右)执行提供的函数。
该方法将数组缩减为单个值。
语法:

array.reduce(function(total(sum), item, index, arr), initialValue)
  • reduce:减少数组元素,
  • sum(求和):用于存储数组元素之和的变量
  • total: 用于求和的变量。
  • item: 接收数组元素的形参,必须。
  • index: 接收数组索引的形参,可选。
  • arr: 接收当前数组的形参,可选。
  • initialValue: 初始值。
    ****示例:
var result = arr.reduce(function (total, item) {
    return total += item
}, 0)
console.log(result) // 6

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容