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

Time:04-11

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

I was writing an if-else condition for checking the max and min value that is maxed or min value contain null or undefined or not if it contains null or undefined then the max or min value should be assigned with " " but writing one condition is good in If else condition

but as you can see here I have written 4 conditions in if statement and it is checking only for null condition but I want for undefined also

I came to know that if I put all my condition in Array list then it will automatically check if the condition satisfy or not

So I wrote all the condition in one Array list but don't know how to call it in If statement to check the condition

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

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

CodePudding user response:

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

If won't as Array.includes() need a parameter to check it in the array on which you call it. You can create an array and test the value of max and min in it.

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

Note: make sure you don't define null as "null" or undefined as "undefined" as they become strings in JS and they are no longer falsey.

A better way to write the above code is:

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,

You don't need if-else if you only want to set min and max.

CodePudding user response:

I don't think that it's the right approach.

You can simplify your conditions in this way

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