I am currently developing a matching algorithm that compares two arrays of strings. If there is an exact match (===), it stores the results in the unSafeResult array. If there is a match using Regex, it stores the results in the warningResult array. However, when I try to console.log(warningResult), I get the following output:
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"]
When attempting to pass the array to another page, only one element is returned:
[undefined × 11, "PALM OIL"]
Is there a solution to this bug? The expected output should be all elements contained within a single array:
"HYDROGENATED VEGETABLE↵OIL"
"RAPESEED OIL"
"PALM OIL"
"SUNFLOWER OIL"
"RAPESEED OIL"
"PALM OIL"
Below are the relevant code snippets:
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(); //issue here
console.log('warning', warningResult);
}
}
}
if(this.counter >0){
this.navCtrl.push(UnSafePage,{unSafeResult,warningResult});
}
else{
this.navCtrl.push(SafePage);
}
}