顾名思义就是 查找传入的对应数据当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
如果没有符合条件的元素返回 undefined

// 定义一个需要被查找的数组
var memoList = [{

id: 1, name: '1'

},{

id: 2, name: '2'

},{

id: 3, name: '3'

}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {

return ele.id == 2

})

// 打印 editItem 得到的结果是 {id: 2, name: '2'}
// 定义一个需要被查找的数组
var memoList = [{

id: 1, name: '1'

},{

id: 2, name: '2'

},{

id: 3, name: '3'

}]
// 用 editItem 变量将 查找出来的数据进行接收
var editItem = memoList.find((ele) => {

return ele.id == 2

})

// 打印 editItem 得到的结果是 {id: 2, name: '2'}这看起来和没什么区别呀。。。但是~~
// 我们修改 接收后的值 editItem 的值
editItem.name = '我是修改后的'

// 我们再打印一下 editItem
console.log( editItem )
// {id: 2, name: '我是修改后的'}
// 我们修改 接收后的值 editItem 的值
editItem.name = '我是修改后的'

// 我们再打印一下 editItem
console.log( editItem )
// {id: 2, name: '我是修改后的'}感觉很正常嘛~然后我们再打印一下 被查找的数据 memoList

console.log(memoList)
// [{id: 1, name: "1"}, {id: 2, name: "我是修改后的"}, {id: 3, name: "3"}]

// 看得没 memoList 内的数据也被修改了
console.log(memoList)
// [{id: 1, name: "1"}, {id: 2, name: "我是修改后的"}, {id: 3, name: "3"}]

// 看得没 memoList 内的数据也被修改了这里就反应出 fine() 方法返回的结果内存指向依然是 memoList 所指向的内存地址所有这里返回的是浅拷贝的数据注意: find() 对于空数组,函数是不会执行的。注意: find() 并没有改变数组的原始值。浏览器支持
浏览器支持表格中的数字表示支持该方法的第一个浏览器版本号。






方法















find()

45.0

12.0

25.0

7.1

32.0








方法















find()

45.0

12.0

25.0

7.1

32.0





方法











方法


find()

45.0

12.0

25.0

7.1

32.0

find()45.012.025.07.132.0注意: IE 11 及更早版本不支持 find() 方法。array.find(function(currentValue, index, arr),thisValue)
参数






参数

描述





function(currentValue, index,arr)

必需。数组每个元素需要执行的函数。

函数参数:









参数

描述





currentValue

必需。当前元素





index

可选。当前元素的索引值





arr

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













thisValue

可选。 传递给函数的值一般用 “this” 值。

如果这个参数为空, “undefined” 会传递给 “this” 值








参数

描述





function(currentValue, index,arr)

必需。数组每个元素需要执行的函数。

函数参数:









参数

描述





currentValue

必需。当前元素





index

可选。当前元素的索引值





arr

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













thisValue

可选。 传递给函数的值一般用 “this” 值。

如果这个参数为空, “undefined” 会传递给 “this” 值





参数

描述

参数描述

function(currentValue, index,arr)

必需。数组每个元素需要执行的函数。

函数参数:









参数

描述





currentValue

必需。当前元素





index

可选。当前元素的索引值





arr

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









function(currentValue, index,arr)必需。数组每个元素需要执行的函数。

函数参数:









参数

描述





currentValue

必需。当前元素





index

可选。当前元素的索引值





arr

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













参数

描述





currentValue

必需。当前元素





index

可选。当前元素的索引值





arr

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









参数

描述





currentValue

必需。当前元素





index

可选。当前元素的索引值





arr

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





参数

描述

参数描述

currentValue

必需。当前元素

currentValue必需。当前元素

index

可选。当前元素的索引值

index可选。当前元素的索引值

arr

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

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

thisValue

可选。 传递给函数的值一般用 “this” 值。

如果这个参数为空, “undefined” 会传递给 “this” 值

thisValue可选。 传递给函数的值一般用 “this” 值。

如果这个参数为空, “undefined” 会传递给 “this” 值以上就是本文的全部内容,希望对大家的学习有所帮助。