Search code examples
javascriptnested-loops

Nested for loop display duplicated output


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?


Solution

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