Javascript 如何在if语句中链接null合并以确定非未定义或null
我的目标是检查是否有任何变量Javascript 如何在if语句中链接null合并以确定非未定义或null,javascript,Javascript,我的目标是检查是否有任何变量未定义或空,同时不检查假值(即空字符串可以,零可以) 我是这样写的: if ( !line?.weight ?? // If weight is defined then... !line?.id ?? // If id is defined !line?.type ?? // If type is defined !line?.coordinates ?? // If coordinates are def
未定义
或空
,同时不检查假值(即空字符串可以,零可以)
我是这样写的:
if (
!line?.weight ?? // If weight is defined then...
!line?.id ?? // If id is defined
!line?.type ?? // If type is defined
!line?.coordinates ?? // If coordinates are defined
false // don't run!
) { // one of these variables is undefined or null
}
我以前把它写成typeof line?.weight!='编号“| |类型…”
我只是问这些问题,看看是否有更好的方法,或者??需要false
。或者如果这真的有效,或者我错过了什么,它会咬我
我想我看得越多,我能否定一个未定义的或空的值来提供一个未定义的或空的值吗?我认为这行不通。这是我能想到的最简单的解决方案。但是看起来您正在寻找的那种需求可能无法通过使用
空合并来实现。希望这对您的需求有所帮助
const checkIfNullOrUndefined=(对象,键)=>{
设布尔=假;
(键| |[])。forEach(键=>{
如果(对象[键]的类型==“未定义”| |对象[键]==空){
布尔=真;
}
});
返回布尔;
}
let line={weight:123}
log(checkIfNullOrUndefined(行,[“权重”]))
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
行={weight:,id:,类型:,坐标:}
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
行={weight:,id:,type:,坐标:undefined}
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
行={weight:,id:,类型:null,坐标:0}
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
line={weight:0,id:0,type:0,座标:0}
log(checkIfNullOrUndefined(line,[“weight”,“id”,“type”,“coordinates”]))
这是我能想到的最简单的解决方案。但是看起来您正在寻找的那种需求可能无法通过使用空合并来实现。希望这对您的需求有所帮助
const checkIfNullOrUndefined=(对象,键)=>{
设布尔=假;
(键| |[])。forEach(键=>{
如果(对象[键]的类型==“未定义”| |对象[键]==空){
布尔=真;
}
});
返回布尔;
}
let line={weight:123}
log(checkIfNullOrUndefined(行,[“权重”]))
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
行={weight:,id:,类型:,坐标:}
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
行={weight:,id:,type:,坐标:undefined}
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
行={weight:,id:,类型:null,坐标:0}
log(checkIfNullOrUndefined(行,[“重量”、“id”、“类型”、“坐标”]))
line={weight:0,id:0,type:0,座标:0}
log(checkIfNullOrUndefined(行、[“重量”、“id”、“类型”、“坐标”))
您可以简单地检查这些值的null
。比如line?.weight==null
使用=
,它将检查null
和undefined
李>
在多个属性的条件之间应用|
将条件包装到(…)
中,并使用而不是代码>条件
最终条件如下所示
if (!(line?.weight == null || line?.id == null || line?.type == null || line?.coordinates == null)) { ... }
您可以在下面检查您的输出
功能测试(行){
if(!(line?.weight==null | | line?.id==null | | line?.type==null | | line?.coordinates==null))
console.log(“真”);
其他的
控制台日志(“假”);
}
let line={weight:123};
测试(线路);//假的
行={权重:,id:,类型:,坐标:};
测试(线路);//真的
行={weight:,id:,type:,坐标:undefined};
测试(线路);//假的
行={weight:,id:,类型:null,坐标:0};
测试(线路);//假的
line={weight:0,id:0,type:0,座标:0};
测试(线路);//true
您可以简单地检查这些值的null
。比如line?.weight==null
使用=
,它将检查null
和undefined
李>
在多个属性的条件之间应用|
将条件包装到(…)
中,并使用而不是代码>条件
最终条件如下所示
if (!(line?.weight == null || line?.id == null || line?.type == null || line?.coordinates == null)) { ... }
您可以在下面检查您的输出
功能测试(行){
if(!(line?.weight==null | | line?.id==null | | line?.type==null | | line?.coordinates==null))
console.log(“真”);
其他的
控制台日志(“假”);
}
let line={weight:123};
测试(线路);//假的
行={权重:,id:,类型:,坐标:};
测试(线路);//真的
行={weight:,id:,type:,坐标:undefined};
测试(线路);//假的
行={weight:,id:,类型:null,坐标:0};
测试(线路);//假的
line={weight:0,id:0,type:0,座标:0};
测试(线路);//true
是否要检查它们是否都是非Null值或其中任何一个是否为非Null值?我认为您理解Null合并的方式不同,如果??
之前的条件为true,则将使用右侧值,即。,如果line?.weight
未定义!行?.weight???
是未定义的
,当weight
具有非null
或未定义的值时。长话短说,只需删除代码>用于conditions@NithishGandesiri我不确定。因为如果第一个不是空的,它就停在那里。。。。这不是我想要的。我希望它检查所有的值,并确保它们都不是空的,也不是未定义的。