if (conv_details.settings.min === null ||
conv_details.settings.min === undefined ||
conv_details.settings.max === null ||
conv_details.settings.max === undefined) {
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: "",
max: "",
});
} else {
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: conv_details.settings.min,
max: conv_details.settings.max,
});
}
我正在编写一个if-else条件,用于检查最大值和min值,如果其中包含空值或未定义值,则min值包含空值或未定义值,如果包含空值或未定义值,则应使用" "
分配max或min值,但在“如果是”条件下,编写一个条件是好的。
但是正如您在这里看到的,我已经在if语句中编写了4个条件,它只检查null
条件,但我也想要undefined
条件
我知道如果我把我所有的条件都放在数组列表中,它就会自动检查条件是否满足
所以我在一个数组列表中写了所有的条件,但是不知道如何调用If语句来检查条件
let testArray=[`conv_details.settings.min === null`,
`conv_details.settings.min === "undefined"`
`conv_details.settings.max === null`
`conv_details.settings.max === "undefined"`];
if(testArray.includes()){
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: "",
max: "",
});
} else {
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: conv_details.settings.min,
max: conv_details.settings.max,
});
}
我不知道在if语句中声明Array.includes以满足所有条件的正确方法,因为我是节点js和JavaScript的新手
发布于 2022-04-09 23:13:10
我不知道在if语句中声明Array.includes以满足所有条件的正确方法,因为我是节点js和JavaScript的新手
如果不是,因为Array.includes()
需要一个参数来在调用它的数组中检查它。您可以创建一个数组并在其中测试max和min的值。
const settingsStatus = [null, undefined]; // don't put in string as these are types in JS
const {min, max} = conv_details.settings;
if(settingsStatus.includes(min) || settingsStatus.includes(max)){
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: "",
max: "",
});
} else {
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: conv_details.settings.min,
max: conv_details.settings.max,
});
}
注意:确保不将null定义为"null“,也不将其定义为”未定义“,因为它们已成为JS中的字符串,并且不再是falsey。
编写上述代码的更好方法是:
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: settingsStatus.includes(min) ? '' : min,
max: settingsStatus.includes(max) ? '' : max,
如果你只想设置min和max,就不需要if -否则。
发布于 2022-04-09 23:12:51
我不认为这是正确的方法。
您可以这样简化您的条件。
const {min, max} = conv_details.settings
if (!min && !max) {
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: "",
max: "",
});
} else {
details_data.push({
field_label: conv_details.label,
description: conv_details.description,
field_name: conv_details.field_name,
content_types: conv_details.bundle,
type: conv_details.field_type,
min: conv_details.settings.min,
max: conv_details.settings.max,
});
}
https://stackoverflow.com/questions/71814433
复制相似问题