0

I have an if statement inside for loop with more than one condition. I want to match the data in database with input data in an HTML form. When the input field in the form is blank it is stored as null in the database. I have this column(itemsSortedByDate[i].FD_MIMO) in the database which can be null or can have some value. I am unable to match the blank field with null in the database. Also even if that column(itemsSortedByDate[i].FD_MIMO) has some value in the database, my for loop searches for the database which has null field just because other fields are matching. My Javascript is as below. The last condition is creating problems. ScenarioListViewModel.fdMimo()and itemsSortedByDate[i].FD_MIMO are supposed to be same whether it's null or has some value. But in the console.log they are different. Thank you for your help, much appreciated.

self.getJobIdForCapacity = function(itemsSortedByDate){
            var jobIdForCapacity;
            var found = false;
            for (var i = 0, len = itemsSortedByDate.length; i < len; i++) {
                if(itemsSortedByDate[i].DB_Name == ScenarioListViewModel.db_name() 
                && itemsSortedByDate[i].Split_Mode == ScenarioListViewModel.splitMode() 
                && itemsSortedByDate[i].Full_Output == ScenarioListViewModel.fullOutput() 
                && (itemsSortedByDate[i].Workflow_Status == "Completed" || itemsSortedByDate[i].Workflow_Status == "Running") 
                && (itemsSortedByDate[i].Disposition == "Success" || itemsSortedByDate[i].Disposition == "None") 
                && (itemsSortedByDate[i].FD_MIMO == ScenarioListViewModel.fdMimo() || itemsSortedByDate[i].FD_MIMO == null)){
                    jobIdForCapacity = itemsSortedByDate[i].Title;
                    console.log("Job Id:" + jobIdForCapacity);
                    console.log("fdmimo from form:" +ScenarioListViewModel.fdMimo());
                    console.log("fdmimo from list:" +itemsSortedByDate[i].FD_MIMO);
                    self.getJobResults(jobIdForCapacity);
                    found = true;
                    break;
                }
            }
            if (!found) {
                alert("Job not found in Sharepoint Execution History List. Click Execute Model to run");
            }   
        };
dpr
  • 35
  • 7
  • Include your `ScenarioListViewModel` code and a sample value of `itemsSortedByDate` so that it will be reproduce-able by others. – Adrian Jul 31 '17 at 04:31

1 Answers1

0

I would suggest you use === in all the conditions in if statement and it may help you solve your problem as there is difference in === vs ==.

Please refer this question for the difference.

For example:

itemsSortedByDate[i].DB_Name == ScenarioListViewModel.db_name()

will be

itemsSortedByDate[i].DB_Name === ScenarioListViewModel.db_name()

Condition:

"Completed" || itemsSortedByDate[i].Workflow_Status == "Running"

will always return "Completed" does not matter itemsSortedByDate[i].Workflow_Status == "Running" is true or false. Here your can use ternary operator like

itemsSortedByDate[i].Workflow_Status == "Running"? "Running" : "Compelted"

Something of this kind. Check all conditions like this.

Dnyanesh
  • 2,265
  • 3
  • 20
  • 17