简体   繁体   English

Array.prototype.find()意外行为

[英]Array.prototype.find() unexpected behavior

According to this MDN link , the find() method takes a callback function that can take three arguments: the current array element, the index of the current array element, and the array that the method is being called upon. 根据此MDN链接 ,find()方法采用一个回调函数,该函数可以采用三个参数:当前数组元素,当前数组元素的索引以及正在调用该方法的数组。

So: 所以:

var r = [2, 9, 11]; console.log(r.find(function(e, i, r) {
    if (e % 2 === 0)
        return e; }))

returns 2, as I would expect. 如我所料,返回2。

However: 然而:

var r = [2, 9, 11];
console.log(r.find(function(e, i, r) {
    if (e % 2 === 0)
        return i;
}))

return undefined (when I expect 0), 返回未定义(当我期望为0时),

and

var r = [2, 9, 11];
console.log(r.find(function(e, i, r) {
    if (e % 2 === 0)
        return r;
}))

returns 2 (when I expect [2, 9, 11]). 返回2(当我期望[2,9,11]时)。

Can someone please explain what I am not properly understanding? 有人可以解释一下我没有正确理解的内容吗?

The callback you pass to .find() is assumed to return a boolean (true or false) value. 假定您传递给.find()的回调返回一个布尔值(真或假)。 The value returned from .find() is the value of the first element for which the callback returned a non-false value. .find()返回的值是回调为其返回非假值的第一个元素的值。

In your first example: 在第一个示例中:

var r = [2, 9, 11]; console.log(r.find(function(e, i, r) {
    if (e % 2 === 0)
        return e; }))

the return value is 2 not because of return e; 返回值是2 不是因为return e; returning the value 2 , but because 2 is truthy. 返回值2 ,但是因为2是真实的。 You can verify that by changing return e to return true or any other truthy value. 您可以通过更改return ereturn true或任何其他真实值来进行验证。

Similarly, in the second example: 同样,在第二个示例中:

var r = [2, 9, 11];
console.log(r.find(function(e, i, r) {
    if (e % 2 === 0)
        return i;
}))

your callback returns 0 on the first element (because i is 0 ), but that's not truthy. 您的回调在第一个元素上返回0 (因为i0 ),但这不是事实。

The find method executes the callback function once for each element present in the array until it finds one where callback returns a true value. find方法对数组中存在的每个元素执行一次回调函数, 直到找到其中回调返回真值的元素为止。

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

The callback return is only used to indicate if the current value should be the result or not. 回调返回仅用于指示当前值是否应为结果。 It's sort of like find asking your callback a "yes or no" question for the current value. 这就好比find您的回调函数对当前值询问“是或否”。

In your second case, 2 passes e % 2 === 0 and you returned i . 在第二种情况下,2通过e % 2 === 0并且您返回i However i is 0 , a falsy value so find skips over that and continues. 但是i0 ,一个虚假的值,所以find跳过它并继续。 Further values (9 and 11) don't pass e % 2 === 0 . 进一步的值(9和11)未通过e % 2 === 0 Functions that don't explicitly return a value return an undefined , a falsy value. 没有显式返回值的函数将返回undefined ,即伪造的值。 In the end, no callback returned a truthy value and find returns undefined . 最后,没有回调返回真实值, find返回undefined

The third case, 2 passes e % 2 === 0 and you returned r , an array, which is a truthy value. 第三种情况, 2传递e % 2 === 0并且您返回了r ,这是一个真实值的数组。 That made find , right off the bat, return the first item which is 2 . 马上find ,返回的第一个项目是2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM