0

I have a form which has lot of fields consider maybe 100. All of the fields are different such as it could be StartTime DateTime EndTime Value etc.. User can fill whichever the field they want and keep the remaining fields black.

Now on the Javascript or Node.js side I need to check if the fields are populated for each of them then based on that I need to create an XML file. As of now, I am trying to check if each field is populated using the if the condition. If I proceed in this method then I need to write 100 IF condition to manage all 100 fields which are time-consuming and redundant.

I was just curious if there is any better way of doing it. I tried searching but could not find any relevant post which gives me some sort of idea. If it's duplicate then I am really sorry.

Does anyone have a better idea?

As of now, I am checking something like this:

if(StartTime  != '' && StartTime  != null && StartTime  != undefined)
{
    append(StartTime)
}

if(DateTime  != '' && DateTime  != null && DateTime  != undefine)
{
    append(DateTime)
}
    
if(EndTime  != '' && EndTime  != null && EndTime  != undefine)
{
    append(EndTime)
}

if(Value  != '' && Value  != null && Value  != undefine)
{
    append(Value)
}

.
.
.
.
BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98
  • Just put the condition inside the `append` function (or a helper function)? – Bergi Jul 22 '20 at 09:55
  • Are these fields available on a collection somewhere? e.g. in Node.js you may have them available on `req.body`, which you can iterate through and then check the value and append it within the same function. If they're not on a collection, then you'll have handle each field individually, but employing some of the other suggestions in this post, such as https://stackoverflow.com/a/63031642/983178 – ourmaninamsterdam Jul 22 '20 at 10:03
  • Thanks a lot for the response. Yes, I am saving the fields in an `array` and sending it over the `req.body` and reading them. In order to simplify the question, I did not include `for loop` etc. – BATMAN_2008 Jul 22 '20 at 10:21

3 Answers3

1

You could do something like this

const appendIf = () => {
   if(val != '' && val != null && val != undefine) {
      append(val);
   }
};

appendIf(StartTime);
appendIf(DateTime);
appendIf(EndTime);
appendIf(Value);

If all the values are in an array or object, you could also just loop over that:

for(/* whatever for loop is needed*/) {
   appendIf(currentValue);
}
MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • Thanks for the response. Actually the first one would work. But only thing is I need to send a lot more parameters so that it can be appended to the exact position within the `XML` document. – BATMAN_2008 Jul 22 '20 at 10:22
  • 1
    Yeah I mean you need to define the order somewhere, so either by making the function calls in order, or by defining the order via an array and looping over that. Something else is not possible, how should the computer know the order without you telling it? @BATMAN_2008 – MauriceNino Jul 22 '20 at 10:33
  • Yup. That's what I meant. I will send the other require parameters and add it within the function itself. – BATMAN_2008 Jul 22 '20 at 11:25
1

I would suggest using a data structure where you can save the data you want to check on, and also if you're checking the same condition for every field then implement the logic in a function. Assuming you're using an array as the data structure the code would look like this:

const appendField = (field) => {
if (field  != '' && field  != null && field  != undefined)
//here you can do something in this case append(field)
}
// myArr is the array where you have the data you want to check
myArr.forEach(field => appendField(field))
kiratot
  • 99
  • 3
1

Looping over the keys in the object and checking the value for each unique key could be a good way to go. If you need to maintain the order of the keys, the forEach method allows you to pass in an index as well. i in the example.

const appendValues = (data) => {
    const keys = Object.keys(data);

    keys.forEach((key, i) => {
        if (data[key]) {
            // Append logic if value
            append(data[key]);
        } else {
           // Logic if no value
        }
    })
};
jamesmallred
  • 206
  • 1
  • 6