[英]Why does this .every() function return true and then false?
let nums = [-1, 50, 75, 200, 350, 525, 1000];
nums.every(function(num) {
console.log(num < 0);
});
true 真正
=> false =>错误
When I run this code in https://repl.it/@super8989/BraveFunctionalSale , this returns "true" then "=> false". 当我在https://repl.it/@super8989/BraveFunctionalSale中运行此代码时,这将返回“ true”,然后返回“ => false”。
According to .every() description, the return value is "true if the callback function returns a truthy value for every array element; otherwise, false." 根据.every()的描述,返回值是“如果回调函数为每个数组元素返回真实值,则为true;否则为false。”
Why does it show "true" and then "=> false"? 为什么显示“ true”然后显示“ => false”?
Furthermore, when I change the array so that "- value" is in the middle of the array, it returns "false" then "=> false". 此外,当我更改数组以使“-value”位于数组中间时,它返回“ false”,然后返回“ => false”。
let nums = [1, 50, -75, 200, 350, 525, 1000];
nums.every(function(num) {
console.log(num < 0);
});
false 假
=> false =>错误
https://repl.it/@super8989/CyberInterestingPhase https://repl.it/@super8989/CyberInterestingPhase
let nums = [-1, 50, 75, 200, 350, 525, 1000];
console.log(nums.every(num => num < 0));
false 假
=> undefined =>未定义
But if I write it this way, this returns false then undefined. 但是,如果我用这种方式编写,则返回false,然后未定义。 https://repl.it/@super8989/MonstrousAjarDimension
https://repl.it/@super8989/MonstrousAjarDimension
I'm very confused... Please help! 我很困惑...请帮助!
You need to use return
inside .every, currently you are just logging num < 0 which is true for num[0] which is (-1 < 0), but false for num[1] which is (50 >= 0). 您需要在.every内部使用
return
,目前,您只记录num <0,这对于num [0]为(-1 <0)为true,但对于num [1]为(50> = 0)为false。 And since you do not return
is instantly exits the loop as this is considered false
. 由于您不
return
因此立即退出循环,因为这被认为是false
。
let numsA = [-1, 50, 75, 200, 350, 525, 1000]; console.log("should return false, because 50 is >= 0"); console.log(numsA.every(function(num) { return (num < 0); })); let numsB = [1, 50, -75, 200, 350, 525, 1000]; console.log(numsB.every(function(num) { return (num < 0); })); let numsC = [-1, 50, 75, 200, 350, 525, 1000]; console.log(numsC.every(num => num < 0)); let numsD = [-5, 0, 5, 10, 15]; console.log("should return true because for each num in numsD, num %5 === 0"); console.log(numsD.every(num => { return num % 5 === 0 })); //you can omit return with arrow function syntax, as this automatically returns the value within () console.log(numsD.every(num => (num % 5 === 0)));
In Array.every method you must provide function that returns true or false . 在Array.every方法中,您必须提供返回 true或false的函数。 Function is being called for every one element in Array.
正在为数组中的每个元素调用函数。 https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/every
https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/every
your function 你的职能
nums.every(function(num) {
console.log(num < 0);
});
returns undefined for every element (missing return statement). 为每个元素返回未定义的值(缺少return语句)。
var resutl = nums.every(function(num) {
return (num < 0);
});
or in es-2017 syntax: 或es-2017语法中:
const resutl = nums.every(num => num < 0);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.