如果内部forEach的匿名函数不起作用 [英] if condition inside forEach's anonymous function does not work
问题描述
pre $函数isUniform(myArray){
myArray.forEach(function(element ){
if(element!== myArray [0]){
return false;
}
});
返回true;
$ b
这个函数应该把一个数组作为输入并返回<$如果数组中的所有元素都是相同的,并且 false
,那么c $ c> true
例如:
isUniform([1,2,1,1]); //返回false
isUniform([1,1,1,1]);但是,如果条件:
if(element!== myArray [0])
在isUniform ([1,2,1,1])。
我缺少什么?
isUniform
返回一个值, ,它会为您提供给 forEach
方法的回调函数返回一个值。 forEach
实际上仅用于创建副作用。所以 forEach
在每个元素上执行回调,看到回调返回 false
,但是没有任何东西用这个值做,所以它抛出并移动到数组中的下一个元素。在遍历数组之后,它将转到下一行代码,并为该函数返回 true
。 一种可能的做法是使用 forEach
来声明一个初始化为true的变量,并在回调中对其进行操作。这是必要的,因为没有办法尽早结束执行 forEach
循环。所以你可以使用:
函数isUniform(myArray){
var passing = true;
myArray.forEach(function(element){
if(element!== myArray [0]){
passing = false;
}
});
回传;
$ / code>
或者您可以使用作为
循环或 for-of
循环,在这种情况下, return
语句将按照您最初的预期。您可能已经熟悉 for
循环。在ES2015中引入了 For-of
循环(所以它们可能不适用于所有JS引擎)。 for
循环看起来像这样:
函数isUniform myArray){
for(element of myArray){
if(element!== myArray [0]){
return false
}
}
返回true
$ / code>
然而,最好的方法是使用内置的数组方法每个
,如果数组中的每个元素都通过回调中提供的测试,则返回 true 。所以你可能会测试每个元素,看它们是否等于数组中的第0个元素,因此相等:
function isUniform(myArray){
return myArray.every(function(currentElement,index,array){
return currentElement === array [0]
))
}
这足够短,您甚至不需要把它放在自己的函数中,如果不这样做的话,代码可能会更具可读性。
文档:
Array.prototype.every: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/ Global_Objects / Array / every
For循环: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of
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.
eg:
isUniform([1,2,1,1]); // should return false isUniform([1,1,1,1]); // should return true
However, the if condition:
if (element !== myArray[0])
never seem to be true in the case of 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. forEach
is really only used to create side effects. 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. After iterating through the array, it moves on to the next line of code and returns true
for the function.
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. This is necessary, as there's not a way to end the execution of a forEach
loop early. 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. You're probably already familiar with for
loops. For-of
loops were introduced in ES2015 (so they might not work on all JS engines). A for-of
loop would look like this:
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. 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:
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
For-of loop: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
这篇关于如果内部forEach的匿名函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!