Array.includes 在 if else 语句中使用节点 js / javascript

Array.includes in if else statement using node js / javascript

 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 条件来检查最大值和最小值是否为最大值或最小值是否包含 null 或 undefined 或者如果它包含 null 或 undefined 那么最大值或最小值应该分配 " " 但在 If else 条件

中写一个条件是好的

但是正如你在这里看到的,我在 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

我不认为这是正确的方法。

你可以这样简化你的条件

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,
     });
   }

I don't know what is the right way to declare the Array.includes in if statement to satisfy all my condition as I am new to node js and JavaScript

如果不会,因为 Array.includes() 需要一个参数来在您调用它的数组中检查它。您可以创建一个数组并测试其中的最大值和最小值。

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”或将 undefined 定义为“undefined”,因为它们在 JS 中成为字符串并且不再是错误的。

上面代码更好的写法是:

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,

如果您只想设置最小值和最大值,则不需要 if-else。