如何在数组中指定多个条件并在 javascript 中的 if 语句中调用它

How to specify multiple conditions in an array and call it in an if statement in javascript

我不知道这是否可行我想将所有条件存储在数组中并需要在 if 语句中调用它

const addition = (...numbers) => {

    let arrayOfTest = [
        `${numbers.length === 0}`,
        `${numbers.some(isNaN)}`,
        `${numbers === null}`,
    ];

    if (arrayOfTest.includes(true)) {
        throw new Error("Invalid Input");
    } else {
        return numbers.reduce((a, b) => {
            return a + b;
        });
    }
};

console.log( addition(1, 3, 4, 5, 7, 8));

这可能吗?我可以将所有条件写在数组列表中并在 if 语句

中调用它吗

不要将布尔值括在反引号中,因为那样会使它们成为字符串。

const addition = (...numbers) => {

let arrayOfTest = [
    numbers.length === 0,
    numbers.some(isNaN),
    numbers === null,
];
if (arrayOfTest.includes(true)) {
    throw new Error("Invalid Input");
} else {
    return numbers.reduce((a, b) => {
        return a + b;
    });
}
};