Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading