I've a scenario that I want to walk in array in javascript, and checking that if the index matches with any option then just PUSH this or print this once. Ive a following array:
["ITEM1", "dummyData1", "dummyData2", "ITEM2", "dummyData1", "dummyData2", "ITEM3", "dummyData1", "dummyData2", "ITEM4", "dummyData1", "dummyData2", "ITEM4", "dummyData1", "dummyData2", "ITEM4", "dummyData1", "dummyData2", "ITEM4", "dummyData1", "dummyData2", "ITEM5", "dummyData1", "dummyData2", "ITEM5", "dummyData1", "dummyData2", "ITEM6", "dummyData1", "dummyData2", "ITEM7", "dummyData1", "dummyData2", "ITEM7", "dummyData1", "dummyData2"]
I want to iterate this array on every THING
and if THING
index matched with the previous then leave this else push in the array.
I try to tackle this scenario using the global variable setting but it wont help.
Desired Output:
[ITEM1 ..... ITEM7]
var currentItem ;
var myArr;
for (var j = 1; j <= 100; j++) {
for (var i = 0; i <= res[j].length-1; i++) {
var option1 = (res[j][i].match(/THING1-/));
var option2 = (res[j][i].match(/THING2-/));
var option3 = (res[j][i].match(/THING3-/));
var option4 = (res[j][i].match(/THING4-/));
var item;
if (option1 != null)
item = "THE_THING-1";
else if (option2 != null)
item = "THE_THING-2";
else if (option3 != null)
item= "THE_THING-3";
else if (option4 != null)
item = "THE_THING-4";
if (currentItem!= item)
{
currentItem = item;
myArr.push("THING"+j)
}
}
}