ES6:数组方法(二) find() findIndex() some() every()

上一篇:ES6:数组方法(一) Array.from() Array.of()

先声明一个测试变量:

const peoples = [
    {name: 'Tom', age: 20},
    {name: 'Jerry', age: 16},
    {name: 'Mario', age: 19},
    {name: 'Yoshi', age: 23}
];

1.find()

find() 方法可以找到数组中符合条件的元素并返回它:

const findMario = peoples.find(people => people.name === 'Mario');
console.log(findMario);

在这里插入图片描述

2. findIndex()

findIndex() 方法可以找到数组中符合条件的元素并返回它的索引:

const findMarioIndex = peoples.findIndex(people => people.name === 'Mario');
console.log(findMarioIndex); //2

3.some()

some() 方法用于检测数组中的元素是否符合条件,当找到第一个 符合条件 的就会终止,并返回true,全部不符合则返回false:

const someFn = peoples.some(people => people.age >= 18);
console.log(someFn); //true

4.every()

every() 方法用于检测数组中的元素是否符合条件,当找到第一个 不符合条件 的就会终止,并返回false,全部符合则返回true:

const everyFn = peoples.every(people => people.age >= 18);
console.log(everyFn);

注意:
以上方法的参数都为以下形式(以every()为例):
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值