JavaScript中filter、map、reduce、find和findIndex的基本使用

一、filter函数

filter(过滤器)起到一个过滤数据的作用,filter中的回调函数有一个要求:必须返回一个布尔值。当返回true时,函数内部会自动回调n进入数组,当返回false时,函数内部会过滤掉这次的n。

  • filter() 不会对空数组进行检测。
  • filter() 不会改变原始数组。
const nums = [10, 20, 30, 40, 100, 510, 300, 70, 999, 60]
let NewNum1 = nums.filter(n => n < 100)
consloe.log(NewNum1)
//输出的结果为 [10, 20, 30, 40, 70, 60]

二、map函数

map()方法定义在JavaScript的Array中,它返回一个新的数组,数组中的元素为原始数组调用函数处理后的值。

注意:

  • map()不会对空数组进行检测
  • map()不会改变原始数组
array.map(function(currentValue, index, arr), thisIndex)

参数:

thisValue:可选。对象作为该执行回调时使用,传递给函数,用作"this"的值。
function(currentValue, index, arr):必须。为一个函数,数组中的每个元素都会执行这个函数。其中函数参数:
currentValue:必须。当前元素的的值。
index:可选。当前元素的索引。
arr:可选。当前元素属于的数组对象。

let array = [1, 2, 3, 4, 5];
let newArray = array.map((item) => {
    
    
    return item * item;
})
console.log(newArray)  // [1, 4, 9, 16, 25]

三、reduce函数

reduce 为数组中的每一个元素依次执行回调函数,不包括数组中被删除或从未被赋值的元素,接受四个参数:初始值(或者上一次回调函数的返回值),当前元素值,当前索引,调用 reduce 的数组。

callback (执行数组中每个值的函数,包含四个参数)
1、previousValue (上一次调用回调返回的值,或者是提供的初始值(initialValue))
2、currentValue (数组中当前被处理的元素)
3、index (当前元素在数组中的索引)
4、array (调用 reduce 的数组)
initialValue (作为第一次调用 callback 的第一个参数。)

array.reduce(function(prev, current, currentIndex, arr), initialValue)

例一(不带initialValue):

var arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
    
    
    console.log(prev, cur, index);
    return prev + cur;
})
console.log(arr, sum);

打印结果:
1 2 1
3 3 2
6 4 3
[1, 2, 3, 4] 10

这里可以看出,上面的例子index是从1开始的,第一次的prev的值是数组的第一个值。数组长度是4,但是reduce函数循环3次。

例二:(带initialValue):

var  arr = [1, 2, 3, 4];
var sum = arr.reduce(function(prev, cur, index, arr) {
    
    
    console.log(prev, cur, index);
    return prev + cur;
}0) //注意这里设置了初始值
console.log(arr, sum);

打印结果:
0 1 0
1 2 1
3 3 2
6 4 3
[1, 2, 3, 4] 10

这个例子index是从0开始的,第一次的prev的值是我们设置的初始值0,数组长度是4,reduce函数循环4次。

结论如果没有提供initialValue,reduce 会从索引1的地方开始执行 callback 方法,跳过第一个索引。如果提供initialValue,从索引0开始。

四、find函数

该方法主要应用于查找第一个符合条件的数组元素。它的参数是一个回调函数。在回调函数中可以写你要查找元素的条件,当条件成立为true时,返回该元素。如果没有符合条件的元素,返回值为undefined。

const myArr=[1,2,3,4,5,6];
var v=myArr.find(value=>value>4);
console.log(v);// 5
没有符合元素,返回undefined:
const myArr=[1,2,3,4,5,6];
var v=myArr.find(value=>value>40);
console.log(v);// undefined

回调函数有三个参数。value:当前的数组元素。index:当前索引值。arr:被查找的数组。

const myArr=[1,2,3,4,5,6];
var v=myArr.find((value,index,arr)=>{
    
    
    return index==4
});
console.log(v);// 5

五、findIndex函数

findIndex()与find()的使用方法相同,只是当条件为true时findIndex()返回的是索引值,而find()返回的是元素。如果没有符合条件元素时findIndex()返回的是-1,而find()返回的是undefined。findIndex()当中的回调函数也是接收三个参数,与find()相同


const bookArr=[
    {
    
    
        id:1,
        bookName:"三国演义"
    },
    {
    
    
        id:2,
        bookName:"水浒传"
    },
    {
    
    
        id:3,
        bookName:"红楼梦"
    },
    {
    
    
        id:4,
        bookName:"西游记"
    }
];
var i=bookArr.findIndex((value)=>value.id==4);
console.log(i);// 3
var i2=bookArr.findIndex((value)=>value.id==100);
console.log(i2);// -1

参考:https://blog.csdn.net/qq_43588799/article/details/107971877

猜你喜欢

转载自blog.csdn.net/weixin_44019523/article/details/113974253