JSArray.prototype.find()

Posted CaiLin907

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JSArray.prototype.find()相关的知识,希望对你有一定的参考价值。

查到符合条件的第一个值

const a = [1,2,3,4]
const b = a.find(i => i>1)    // 查到i>1的第一个值  b的值为2

示例:

用对象的属性查数组的对象

已知a数组中有一个对象的name为'小蓝',查出该对象

const a = [name: '小明',age: 20,
     name: '小红',age: 21,
     name: '小蓝',age: 22]

方法一,箭头函数:

const res = a.find(i => i.name === '小蓝')    // 查到name为'小蓝'的对象 

方法二,解析构值:

const res = a.find((name) => name === '小蓝')

两种方法打印的res值都为 name: '小蓝',age: 22

参考:

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/find

以上是关于JSArray.prototype.find()的主要内容,如果未能解决你的问题,请参考以下文章