GX-X
GX-X

Reputation: 115

Storing array[i] from forloop to a new array returning undefined

I am currently working on a matching algorithm that would match an array of strings against another array of string.If it match completely (===) it will store the results into unSafeResult array.If it matches through Regex , it will store the results inside warningResult array.However when i console.log(warningResult).It shows

warning (2) [undefined × 1, "SUGAR.↵CHOCOLATE LIQUOR"]
main.js:41867 warning (3) [undefined × 2, "HYDROGENATED VEGETABLE↵OIL"]
main.js:41867 warning (6) [undefined × 5, "RAPESEED OIL"]
main.js:41867 warning (7) [undefined × 6, "PALM OIL"]
main.js:41867 warning (8) [undefined × 7, "SUNFLOWER OIL"]
main.js:41867 warning (11) [undefined × 10, "RAPESEED OIL"]
main.js:41867 warning (12) [undefined × 11, "PALM OIL"]

And then if try to pass the array to another page it only returns 1 element only which is

[undefined × 11, "PALM OIL"]

Is there any way that i can solve this bug? The expected output should be all of them should be under one array

"HYDROGENATED VEGETABLE↵OIL"
"RAPESEED OIL"
"PALM OIL"
"SUNFLOWER OIL"
"RAPESEED OIL"
"PALM OIL"

below is the codes

matchText(array){        
    for(var i = 0; i < 1; i++) {

        var label = this.labels[i];

        var ingredients = label.description.toString().split(/[(,)]/igm).map(function (ingredients){return ingredients.trim()}, {});

        let ingredientList:string[] = ingredients;
        let ingredientUpdatedList:string[];

        for(var k = 0; k<ingredientList.length; k++){
        if(ingredientList[k] === ""){
            ingredientList = ingredientList.filter((ingredientList) =>{
                return ingredientList.trim() != '';
            });
        }
    }


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

        let allergy:string[] = ["OIL","SUGAR"];

        var unSafeResult = [];

        for(var e = 0; e<allergy.length; e++) {
            var regexp = new RegExp(allergy[e], "igm");

            if(ingredientList[j] == allergy[e]){

                unSafeResult[j] = ingredientList[j];   
                console.log('unSafe',unSafeResult);
            }

            if(ingredientList[j].match(regexp)){
                this.counter++;

                var warningResult = [];

                warningResult[j] = ingredientList[j].valueOf(); //here is the issue

                console.log('warning', warningResult);

                //console.log('Match');
                //console.log('Ingredients that matched',unSafeResult);
            }
            else {
                //console.log('No Match');

            }
        }

    }

    if(this.counter >0){
        this.navCtrl.push(UnSafePage,{unSafeResult,warningResult});
    }
    else{
        this.navCtrl.push(SafePage);
    }


}

Upvotes: 1

Views: 77

Answers (1)

Pengyy
Pengyy

Reputation: 38189

Reason about undefined:

When you define an empty array, and set something to the array by index large than 0, it will be filled with undefined automatically until the index you used.

Example to confirm:

var testArr = [];

testArr[5] = 'test';

console.log(testArr);

Solution:

You should define warningResult out of for-loop, and push items to it according to your other conditions.

var ingredientList = ["HYDROGENATED VEGETABLE↵OIL",
  "RAPESEED OIL",
  "PALM OIL",
  "SUNFLOWER OIL",
  "RAPESEED OIL",
  "PALM OIL"
];

var warningResult = [];

for (var j = 0; j < ingredientList.length; j++) {
  // only aviable in for loop
  //var warningResult = [];
  //warningResult[j] = ingredientList[j].valueOf();

  warningResult.push(ingredientList[j].valueOf());

}

console.log('warning', warningResult);

Upvotes: 3

Related Questions