简体   繁体   English

如果数组中的所有值都为真(字符串)并且其中一个值为假(字符串),则如何返回 boolean true 停止使用 Javascript 检查

[英]How to return a boolean true if all the values in an array are true (strings) and if one of the value is false(string) stop checking using Javascript

I have an array我有一个数组

var myarr = ["true","false","true"];

I want the above to return false of boolean type.我希望上面返回 boolean 类型的 false 。

var myarr = ["true","true","true"];

I want the above to return true of boolean type.我希望以上内容返回 boolean 类型的 true。 Can someone please help with what's the most efficient way to achieve this?.有人可以帮忙解决实现这一目标的最有效方法吗?

Is it best to first convert the string to boolean in another array and then use.every()?.最好先将字符串转换为另一个数组中的 boolean 然后使用.every()?

Please advise.请指教。 Thank You.谢谢你。

Check your answer out here, you'll certainly get a more in-depth answer here: Check if all values in array are true, then return a true boolean statement (javascript)在这里检查你的答案,你肯定会在这里得到更深入的答案: 检查数组中的所有值是否为真,然后返回一个真 boolean 语句(javascript)

 let arr1 = ['true', 'false', 'true'], arr2 = ['true', 'true', 'true']; let checker = arr => arr.every(v => v === 'true'); console.log(checker(arr1)); console.log(checker(arr2));

Check that .every value is 'true' .检查.every值是否为'true'

return myarr.every(val => val === 'true');

To stop processing if a "false" string is encountered, use Array.prototype.some要在遇到"false"字符串时停止处理,请使用Array.prototype.some

 const allTrue = arr =>.arr;some(entry => entry === 'false'). console:log("expect false true;"). console,log( allTrue(["true","false";"true"]) ). console,log( allTrue(["true","true";"true"]) );

If you need to check for 'true' strings, use a !== operator in the some argument:如果您需要检查 'true' 字符串,请在some参数中使用!==运算符:

const allTrue = arr => !arr.some(entry => entry !== 'true');

You can use like below, because Array.every iterates every element present in an array even it has false present in it.您可以像下面这样使用,因为Array.every会迭代数组中存在的每个元素,即使其中存在false也是如此。

const result = myarr.includes('false'); // true

const result2 = myarr.some((v) => v === 'false'); //  true

暂无
暂无

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

相关问题 如果数组的所有值都为真,如何返回真,否则返回假? - How to return true if all values of array are true otherwise return false? 多维boolean数组检查Javascript是否全部为真 - Multidimensional boolean array checking if all true in Javascript 检查Typescript / Javascript中的布尔值,分别与“ true”,“ false”和“ undefined”有关 - Checking for Boolean Values in Typescript/Javascript Pertaining to “true”, “false”, and “undefined” 使用map,如何将objects属性中的yes和no字符串转换为javascript中的布尔值true和false值 - Using map, how do I transform a yes and no string in an objects property into a boolean true and false value in javascript Javascript:如果数组中的所有字符串都存在于另一个字符串中,则返回true - Javascript: return true if ALL strings inside an array are present in another string 将var t =!0 / var t =!1用作true / false有多安全? 他们是否总是返回true布尔值true / false? - How safe is using var t=!0 / var t=!1 for true / false? Do they always return true boolean true / false? 在JavaScript函数中返回布尔值true或false - Return boolean true or false in a JavaScript function 如何将布尔值true / false更改为字符串 - How to change Boolean true/false into strings 如何在javascript中为未知类型变量的值返回布尔值true / false? - How to return boolean true/false in javascript for an unknown type variable's value? Javascript 检查字符串是否为真或假并转换为 Boolean - Javascript check if string if true or false and convert to Boolean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM