Destructure array in conditional

Tom*_*m M 1 javascript arrays if-statement destructuring ecmascript-6

我有一个 api,它在数组中返回一个布尔值。如何在条件中解构变量?

let condition = [true];


if (...condition) {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用,condition[0]但解构解决方案似乎更合理,因为如果数组包含多个值,则可以对每个值进行评估 ( let condition = [true, true, true])。

Kir*_*gin 6

您可以使用Array.prototype.every

> [true, true].every(x => x)
true
> [true, false].every(x => x)
false
Run Code Online (Sandbox Code Playgroud)

所以:

let condition = [true, true, true];
if (condition.every(x => x)) {
    // do stuff
}
Run Code Online (Sandbox Code Playgroud)