Javascript - 如何使用 AND (&&) 运算符获取 if 条件中的键值
Javascript - how to get the key value in if condition using AND (&&) operator
有没有可能得到这样的数据
我的多个 And Or 条件示例代码
var dataset = { "dataone": "dataonevalue", "datatwo": "datatwovalue", "datathree:"" }
switch (dataset.dataone && dataset.datatwo && dataset.datathree) {
case "":
case null:
case undefined:
console.log("data missing")
break;
default:
dataset.dataone = "dataonevalue"
dataset.datatwo = "datatwovalue"
dataset.datathree = "datathreevalue"
}
我必须显示哪个键有 "" 或 null 或 undefined 并且我必须获得在 Json object
中找到这三个中的任何一个的特定键
比如这样
console.log("data missing in datathree")
该怎么做?...任何人都可以帮我解决这个问题吗?
I have to show which key has the "" or null or undefined and I have to get the particular key in which any of the three is found in Json object
试试这个:
let dataset = {
"dataone": "dataonevalue",
"datatwo": "",
"datathree": null
};
for (const item in dataset) {
let value = dataset[item];
if (value === "") {
console.log(`"${item}" is empty`);
} else if (value === null) {
console.log(`"${item}" is null`);
} else if (value === undefined) {
console.log(`"${item}" is undefined`);
}
}
希望对您有所帮助!
有没有可能得到这样的数据
我的多个 And Or 条件示例代码
var dataset = { "dataone": "dataonevalue", "datatwo": "datatwovalue", "datathree:"" }
switch (dataset.dataone && dataset.datatwo && dataset.datathree) {
case "":
case null:
case undefined:
console.log("data missing")
break;
default:
dataset.dataone = "dataonevalue"
dataset.datatwo = "datatwovalue"
dataset.datathree = "datathreevalue"
}
我必须显示哪个键有 "" 或 null 或 undefined 并且我必须获得在 Json object
中找到这三个中的任何一个的特定键比如这样
console.log("data missing in datathree")
该怎么做?...任何人都可以帮我解决这个问题吗?
I have to show which key has the "" or null or undefined and I have to get the particular key in which any of the three is found in Json object
试试这个:
let dataset = {
"dataone": "dataonevalue",
"datatwo": "",
"datathree": null
};
for (const item in dataset) {
let value = dataset[item];
if (value === "") {
console.log(`"${item}" is empty`);
} else if (value === null) {
console.log(`"${item}" is null`);
} else if (value === undefined) {
console.log(`"${item}" is undefined`);
}
}
希望对您有所帮助!