简体   繁体   English

如果forEach的匿名函数中的条件不起作用

[英]if condition inside forEach's anonymous function does not work

Consider the code snippet below: 考虑下面的代码片段:

function isUniform(myArray) {
    myArray.forEach(function(element) {
        if (element !== myArray[0]) {
            return false;
        }
    });

    return true;
}

The intention is that the function should take an array as input and return true if all the elements in the array are the same and false otherwise. 目的是该函数应将数组作为输入,如果数组中的所有元素都相同,则返回true否则返回false

eg: 例如:

isUniform([1,2,1,1]); isUniform([1,2,1,1]); // should return false isUniform([1,1,1,1]); //应该返回false isUniform([1,1,1,1]); // should return true //应该返回true

However, the if condition: 但是,如果条件:

if (element !== myArray[0])

never seem to be true in the case of isUniform([1,2,1,1]). 在isUniform([1,2,1,1])的情况下,似乎永远都不是真的。

What is it that I am missing ? 我想念的是什么?

So the return true isn't returning a value for the function isUniform , it's returning a value for the callback that you provided to the forEach method. 因此, return true不会返回函数isUniform的值,而是返回您提供给forEach方法的回调的值。 forEach is really only used to create side effects. forEach实际上仅用于创建副作用。 So forEach executes the callback on each element, sees that the callback returns false , but then doesn't have anything to do with that value, so it throws it out and moves on to the next element in the array. 因此, forEach在每个元素上执行回调,看到该回调返回false ,但是与该值没有任何关系,因此将其丢弃并将其移至数组中的下一个元素。 After iterating through the array, it moves on to the next line of code and returns true for the function. 遍历数组后,它将继续执行下一行代码,并对该函数返回true

One way that you might do this using forEach is to declare a variable that you initialize as true and manipulate that within the callback. 使用forEach进行此操作的一种方法是,声明一个初始化为true的变量,然后在回调中进行操作。 This is necessary, as there's not a way to end the execution of a forEach loop early. 这是必要的,因为没有办法尽早结束forEach循环的执行。 So you might instead use: 因此,您可以改为使用:

function isUniform(myArray) {
    var passing = true;
    myArray.forEach(function(element) {
        if (element !== myArray[0]) {
            passing = false;
        }
    });

    return passing;
}

Or you could use a for loop or a for-of loop, in which case the return statement would work as you had originally expected. 或者,您可以使用for循环或for-of循环,在这种情况下, return语句将按您最初的预期工作。 You're probably already familiar with for loops. 您可能已经熟悉for循环。 For-of loops were introduced in ES2015 (so they might not work on all JS engines). For-of循环是在ES2015中引入的(因此它们可能不适用于所有JS引擎)。 A for-of loop would look like this: for-of循环如下所示:

function isUniform(myArray) {
    for (element of myArray) {
        if (element !== myArray[0]) {
            return false
        }
    }
    return true
}

However, the best way to do this is probably using the built in array method every , which returns true if every element in the array passes the test provided in the callback. 但是,执行此操作的最佳方法可能是使用内置数组方法every ,如果数组中的每个元素都通过了回调中提供的测试,则返回true So you might test every element to see if they're equal to the 0th element in the array and thus equal to each other: 因此,您可以测试每个元素以查看它们是否等于数组中的第0个元素,从而彼此相等:

function isUniform(myArray) {
    return myArray.every(function (currentElement,index,array) {
        return currentElement === array[0]
    })
}

That's short enough that you really don't even need to put it in its own function -- and your code will probably be more readable if you don't. 这足够短,您甚至根本不需要将其放在自己的函数中,如果不这样做,您的代码可能会更具可读性。

Docs: Array.prototype.every: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every 文件:Array.prototype.every: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every

For-of loop: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of For-of循环: https//developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of

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

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