developer
developer

Reputation: 658

Loop Iteration avoid same indexes javascript

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)
}
}
}

Upvotes: 0

Views: 307

Answers (3)

Cristik
Cristik

Reputation: 32806

Try this:

// add a "i" at the end to make the regex case insensitive
// i.e.: /^ITEM\d+$/i
var regex = /^ITEM\d+$/;

var foundItems = {};
filteredItems = myArr.filter(function(item) {
    if(regex.test(item) && !foundItems.hasOwnProperty(item)) { 
        foundItems[item] = true;
        return true;
    }
    return false;
});

On IE the filter function is available only from IE9. If you need to support older versions of IE you'll need to iterate over the array.

The array unique elegant solution found at Unique values in an array.

Upvotes: 0

royhowie
royhowie

Reputation: 11171

Given an array arr:

["ITEM1", "dummyData1", "dummyData2", "ITEM2", ... ]

The easiest way to find the set of all members of the form ITEMc, where c is a constant would be to mark a placeholder array at the appropriate indices:

var getUniqueSetOfItems = function (arr) {
    // our placeholder array
    var p = [];
    // loop through every element in `arr`
    for (var i = 0; i < arr.length; i++) {
        // if the first four characters of `arr[i]` are "ITEM"
        if (arr[i].slice(0,4) == "ITEM")
            // then set our placeholder array at that index to true
            p[i] = true;
    }
    // filter the placeholder array for all indices marked true
    // then map the filtered array to its indices
    return p.filter(function (d) { return d; }).map(function (d, i) { return i; });
}

This solution has the benefit of having O(n) time complexity with only O(n) additional space. The other easiest solution I can think of (looping through the array to check whether the next element is already there) would be much slower, at O(n^2).

Upvotes: 1

victor175
victor175

Reputation: 624

Is this what you wish to do:

var array = [],
  data = ['dummydata1', 'dummydata2'],
  itemNameBasis = "ITEM";


for(var i = 1; i < 8; i++) {
   var currentItemName = itemNameBasis + i;
   // here you may change data for different items depending on the name
   if(array.indexOf(currentItemName) == -1) {
      array.push(currentItemName);
      array = array.concat(data);
   }
}

console.log(array);

Upvotes: 0

Related Questions