简体   繁体   English

为什么此.every()函数返回true然后返回false?

[英]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/Cyber​​InterestingPhase


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.

相关问题 为什么此函数返回true而不是false - Why does this function return true instead of false 为什么“[] == 0”返回true而“[]”为真且“0”为假? - why does “[] == 0” return true while “[]” is true and “0” is false? 为什么lodash`_.all([true,true,true],true);`return`false`? - Why does lodash `_.all([true, true, true], true);` return `false`? 为什么这个函数只返回真,因为没有 else 语句使它在非真条件下返回假? - Why does this function return only true, since there is no else statement to make it return false under a non true condition? 为什么我的 `if(result !== &quot;false&quot;)` 返回 true? - Why does my `if(result !== "false")` return true? 为什么这个函数返回true? - Why does this function return true? 为什么JS中的true ==&#39;true&#39;语句返回false? - Why does true == 'true' statement in JS return false? 为什么jquery有一个函数“returnTrue()”和“returnFalse()”只返回true和false? - Why does jquery have a function “returnTrue()” and “returnFalse()” that just return true and false? 为什么此功能有效? 不应在每次调用该函数时“执行”都返回false吗? - Why does this function work? Shouldn't “executed” return to false every time the function is called? 为什么此表达式在Firefox / Chrome中返回true,而在IE中返回false? - Why does this expression return true in Firefox/Chrome and false in IE?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM