0

I have been getting the output but it is twice the amount.Like for instance if the desired output is SUGAR.The program will display

SUGAR
SUGAR

Is there anyway that i can fix this issue? i am actually using a nested for loop to loop through an array of objects.Below are the codes for the nested for loop

for(var i=0; i<ingredientList.length; i++){

     for(var j=0; j<this.objUser.length; j++){

          len = this.objUser.length;

        userAllergyDetails.push(this.objUser[j].userAllergies);

       for(var k=0; k<len; k++){  //matching starts

         if(ingredientList[i] == this.objUser[j].userAllergies.toUpperCase()){

           console.log('match');
           unSafeResult.push(ingredientList[i]);
           console.log(unSafeResult);
         }

       }


     }

   }
    console.log(userAllergyDetails);
   console.log(userAllergyDetails.length);
   console.log(this.objUser);

  }

The output for the above code is

ingredient list (26) ["INGREDIENTS↵WHEAT FLOUR", "SUGAR", "CHOCOLATE LIQUOR", "HYDROGENATED VEGETABLE↵OIL", "SAL FAT", "SHEA BUTTER", "RAPESEED OIL", "PALM OIL", "SUNFLOWER OIL", "WHOLE MILK↵POWDER", "SHORTENING", "COCOA BUTTER", "SALT", "BUTTER", "YEAST", "EMULSIFIER", "POLYGLYCEROLPOLYRICINOLEANTE", "SOY LECITHIN", "ARTIFICIAL FLAVOR", "TRISODIUMPHOSPHATE", "LEAVENING", "SODIUM BICARBONATE", "CONTAINS: MILK", "WHEAT", "SOYBEANS and SHEANUT", "Manufactured in a facility that uses↵EGGS and PEANUTS"]
 match
main.js:41963 ["SUGAR"]0: "SUGAR"1: "SUGAR"length: 2__proto__: Array(0)
main.js:41961 match
main.js:41963 (2) ["SUGAR", "SUGAR"]0: "SUGAR"1: "SUGAR"length: 2__proto__: Array(0)
main.js:41968 data retrieve from object (52) ["Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar", "Oil", "Sugar"]

and this is the object that i am trying to loop through

Object
0:
resultUnsafe
:
Array(0)
resultWarning
:
Array(0)
userAllergies
:
"Oil"
userName
:
"Dad"
__proto__
:
Object
1
:
Object
resultUnsafe
:
Array(0)
resultWarning
:
Array(0)
userAllergies
:
"Sugar"
userName
:
"Mum"

As shown in the object, there is only 2 userAllergies which means the length would be 2 however why is it showing 52 in the console? and how do i make sure that only SUGAR only is being pushed to the array?

2
  • You should check why you're never actually using your loop index k. Commented May 18, 2017 at 15:08
  • i intended to use the index k to push in the unSafe result to the object , however before i started on that im faced with this bug.UnSafeResult above is currently for now used as testing purpose Commented May 18, 2017 at 15:14

1 Answer 1

1

To make your code better you should consider using more descriptive index variables:

for (var ingredientIndex = 0; ingredientIndex < ingredientList.length; ingredientIndex ++)  

It takes longer to type, but is easier to debug subtle problems like the one you are having

Here is version that works, and is easier to read:

// Loop through the ingredients
for (var ingredientIndex = 0; ingredientIndex < ingredientList.length; ingredientIndex++) {

    // For each ingredient, check each user to see if they are allergic
    for (var userIndex = 0; userIndex < objUser.length; userIndex++) {

        // Loop through each user's allergies
        for (var allergyIndex = 0; allergyIndex < objUser[userIndex].userAllergies.length; allergyIndex++) {
            if (ingredientList[ingredientIndex] == objUser[userIndex].userAllergies[allergyIndex].toUpperCase()) {
                unSafeResult.push(ingredientList[ingredientIndex]);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

regarding the inner most loop , is it the one with var k? or the one with var j? I tried switching objUser[j] to [k].The issue still persist.
In the 'k' loop you were looping from 0-1 (length of the user array), but testing user index 'j' (dad) both times. So sugar got added twice. You should have been looping through the allergies for each user. I've added a functioning example to my answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.