首页 / 科技百科 / 正文

js数组的find方法 

js数组的find方法返回数组中满足条件的第一个元素的值,否则返回undefined。find方法对数组中的每个元素执行callback函数,并返回true的第一个元素值。不会改变原数组。

find方法的语法是:array.find(function(currentValue, index, arr), thisValue)。其中currentValue为当前项,index为当前索引,arr为当前数组。

例如,对于数组[1,2,3,4,5],执行test.find(item => item > 3)将返回4,因为4是第一个大于3的元素。如果执行test.find(item => item == 0),将返回undefined,因为数组中没有等于0的元素。

如有侵权请及时联系我们处理,转载请注明出处来自