javascript 的筛选方法(多种方法细解)

filter() 是一个数组方法,于创建一个新的数组,其中包含原始数组中满足指定条件的所有元素。

返回满足条件的所有内容 放在新的数组里

const numbers = [1, 2, 3, 4, 5, 6];
const evenNumbers = numbers.filter(function(num) {
  return num % 2 === 0;
});
console.log(evenNumbers); // [2, 4, 6]

find() 是一个数组方法,用于在数组中查找满足特定条件的第一个元素,并返回该元素的值

遍历数组中所有内容 找到第一个符合值返回(返回一个对象) 否则为 undefined

const items = [
  { name: 'apple', price: 2 },
  { name: 'banana', price: 3 },
  { name: 'pear', price: 2 }
];
const item = items.find(item => item.name === 'banana');
console.log(item); // { name: 'banana', price: 3 }

some() 检测数组中是否有符合指定条件的元素,如果有则返回true,否则返回false。

一真则真

const numbers = [1, 2, 3, 4, 5];
const hasEvenNumber = numbers.some(number => number % 2 === 0);
console.log(hasEvenNumber); // true

every() 检测数组中是否每个元素都符合指定条件,如果是则返回true,否则返回false

一假则假

const numbers = [2, 4, 6, 8, 10];
const isEvenNumbers = numbers.every(number => number % 2 === 0);
console.log(isEvenNumbers); // true

reduce() 对数组中所有元素进行累加或者其他操作,最终返回一个结果

所有内容进行累加

const numbers = [1, 2, 3, 4, 5];  //累加
const sum = numbers.reduce((accumulator, currentValue) => accumulator + currentValue);
console.log(sum); // 15

const arr = [   //去重
  { name: '内容1', id: 1 },
  { name: '内容2', id: 2 },
  { name: '内容2', id: 2 },	
  { name: '内容3', id: 3 },
  { name: '内容4', id: 4 },
]
const arr1 = arr.reduce((prev, cur) => {
  prev[cur.id] = cur
  return prev
}, {})
console.log(arr1)// 去除了 id:2的对象 当前返回的值

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

total

必需。initialValue,或函数先前返回的值。

currentValue

必需。当前元素的值。

index

可选。当前元素的数组索引。

arr

可选。当前元素所属的数组对象

initialValue

可选。作为初始值传递给函数的值。

reduce()方法详解:是 JavaScript 数组的一个高阶函数,它可以对数组中的元素进行迭代,并返回一个单一的值。它接收两个参数:一个是用于迭代的函数,另一个是初始值。

在 reduce() 方法中,第一个参数是一个匿名函数,它接收两个参数:prev 和 cur。prev 表示上一次迭代的返回值,而 cur 则表示当前迭代的元素。在第一次迭代时,prev 的值可以是数组的第一项值,也可以是 reduce() 方法的第二个参数值。如果没有指定第二个参数,prev 的值就是数组的第一项值,cur 的值就是数组的第二项值。

在匿名函数中,我们可以对 prev 和 cur 进行任何操作,并返回一个新的值。这个新的值将会成为下一次迭代的 prev 值。当迭代完成后,reduce() 方法会返回最终的 prev 值。

总之,reduce() 方法可以使用一个匿名函数对数组中的元素进行迭代,并返回一个单一的值。在匿名函数中,我们可以自定义 prev 和 cur 的值,并返回一个新的 prev 值,用于下一次迭代。

includes()方法用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false

语法:includes(searchElement,fromIndex)

searchElement

必须。需要查找的元素值。

fromIndex

可选。从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。

当fromIndex 参数负值,则按升序从 array.length + fromIndex 的索引开始 搜索

var arr = [1,2,3,4];
arr.includes(1,-1); //false
arr.includes(2,-4); //true
因为arr.length = 4
第一个: arr.includes(1,4+(-1) ) => arr.includes(1,3) 从索引3开始,所以没有找到元素返回false
第二个: arr.includes(2,4+(-4)) => arr.includes(2,0) 也就是从索引0开始,找到元素返回true

 indexof() 方法查找到第一个传入的元素,并返回当前元素的索引,如果没找到就就返回-1。

语法:arr.indexOf(searchElement[, fromIndex])

开始查找的位置。如果该索引值大于或等于数组长度,意味着不会在数组里查找,返回-1。如果参数中提供的索引值是一个负值,则将其作为数组末尾的一个抵消,即-1表示从最后一个元素开始查找,-2表示从倒数第二个元素开始查找 ,以此类推。 注意:如果参数中提供的索引值是一个负值,并不改变其查找顺序,查找顺序仍然是从前向后查询数组。如果抵消后的索引值仍小于0,则整个数组都将会被查询。其默认值为0.

let arr1 = ['a', 'b', 'c']

const r1 = arr1.myIndexOf('a')

console.log(r1) // 0

const r2 = arr1.myIndexOf('a', 1)

console.log(r2) // -1

const r3 = arr1.myIndexOf('a', -1)

console.log(r3) // -1

const r4 = arr1.myIndexOf('c')

console.log(r4) // 2

const r5 = arr1.myIndexOf('c', 1)

console.log(r5) // 2

const r6 = arr1.myIndexOf('c', -1)

console.log(r6) // 2

 findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

在数组中 找到则返回第一个下标,没找到则返回-1

1.当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。

2.如果没有符合条件的元素返回 -1

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值