gpt4 book ai didi

javascript - 将多个 && 条件合并为 1

转载 作者:行者123 更新时间:2023-11-28 14:27:35 28 4
gpt4 key购买 nike

Javascript 中有没有更好的方法对我想要的多个元素进行 null 检查

a=null; b=null;....n=null;

因此,我总共有 14 个变量最初设置为 null,如果它们为“null”,我只想对所有变量应用一种解决方案。

if (a=== null && b=== null&& c=== null&& d=== null&& e=== null &&...&& n===null){
// set some field to false
}

是否有更好的方法来检查 null,因为如果我们添加更多数组,这可能会变得很麻烦谢谢!

最佳答案

创建一个数组数组,使用 Array.every() 对其进行迭代,如果所有项目值为 null,则执行某些操作:

if([a, b, c, ... n].every(a => a === null)) {
// set some field to false
}

关于javascript - 将多个 && 条件合并为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52655689/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com