循环迭代避免使用相同的索引javascript

时间:2015-04-14 18:59:55

标签: javascript arrays

我希望在javascript中使用数组进行数组操作,并检查索引是否与任何选项匹配,然后只是将其推送或打印一次。 我有一个以下数组:

["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"]

我想在每个THING上迭代这个数组,如果THING索引与前一个匹配,那么将其他数据推入数组中。 我尝试使用全局变量设置来解决这个问题,但它不会有帮助。

期望的输出:

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

3 个答案:

答案 0 :(得分:1)

给定数组arr

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

找到ITEMc形式的所有成员集合的最简单方法,其中c是常量,将在适当的索引处标记占位符数组:

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

此解决方案的好处是O(n)时间复杂,只有O(n)个额外空间。我能想到的另一个最简单的解决方案(循环遍历数组以检查下一个元素是否已存在)会慢得多,O(n^2)

答案 1 :(得分:0)

这是你想做的事情:

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

答案 2 :(得分:0)

试试这个:

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

在IE上,过滤功能仅在IE9中可用。如果您需要支持旧版本的IE,则需要迭代数组。

Unique values in an array找到阵列独特的优雅解决方案。