在打字稿中是否存在布尔列表的“全部”或“任何”运算符

时间:2020-02-19 14:01:51

标签: typescript boolean-operations

我想写一个形式为if的语句

if (all entries in booleanList are true) {do something}

只需放入booleanList即可完成.any()

我在互联网或打字本上找不到答案。预先感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您可以使用Array.prototype.every()函数:

if(booleanList.every(val => val)){
 do something
}

答案 1 :(得分:2)

好的,它是every

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

const list = [true, false, true];

const every = list.every(bool => bool === true); // false
// const every = list.every(bool => bool); // false

答案 2 :(得分:2)

Typescript可编译为Javascript,因此它在运行时没有Javascript没有的任何功能。但是,自ES5起,只要您传递身份函数,Javascript便同时具有everysome

arr.every(x => x) // all
arr.some(x => x)  // any

这类事情不太可能在Typescript书籍中进行专门记录,因为不必重复Javascript书籍的内容,只需说Typescript也具有这些功能即可。如果您想了解标准库中可用的功能,请查阅Javascript参考,例如MDN Javascript reference