检查数组项与另一个数组

时间:2011-06-07 23:30:46

标签: javascript arrays testing

我正在尝试测试数组中的值并根据该结果进行相应的操作,即,如果测试的数组中不存在该项,则将其添加到数组中。我已经花了太多时间在这上面,我真的可以使用一些帮助。

    function FilterItems(attrName, attrValue, priceMin, priceMax) {
        // check if item exists in filtered items
        for (var i = 0; i < adlet.item.length; i++) {  
            if (adlet.item[i][attrName] == attrValue) {
                var currentItem = adlet.item[i];
                if (filteredItems.length > 0) {
                    // console.log(filteredItems.length);
                    for (var x = 0; x < filteredItems.length; x++) {                       
                        if (filteredItems[x].OMSID == currentItem.OMSID) {  
                            // match found
                            break;           
                        } else {
                            // match not found, add to filtered items.
                            filteredItems.push(currentItem);
                        }
                    }          
                } else {
                    filteredItems.push(adlet.item[i]);
                    // console.log(filteredItems.length);
                }
            }
        }

3 个答案:

答案 0 :(得分:1)

您在每次迭代中添加currentItem,但未找到它。 如果找不到,则必须在循环后调用filteredItems.push(currentItem);

...

    var found = false; // new 'found' var = true if currentItem is found
    for (var x = 0; x < filteredItems.length; x++) {
        if (filteredItems[x].OMSID == currentItem.OMSID) {
            // match found
            found = true;
            break;
        }
    }

    // match not found, add to filtered items.
    if (!found) {
        filteredItems.push(currentItem);
    }

} else {
    filteredItems.push(adlet.item[i]);
    // console.log(filteredItems.length);
}

...

答案 1 :(得分:1)

/ * 如果对数组执行任何操作,则数组方法'indexOf'太有用了,无法忽略。根据Mozilla的代码,这里有一个垫片。

此处显示的'add'方法与push类似,但只有在项目不在数组中时才添加项目。您可以添加多个带有多个参数的项目。

'merge'会将调用数组中的所有'new'项添加到作为参数发送的数组中, * /

if(!Array.prototype.indexOf){
    Array.prototype.indexOf= function(what, i){
        i= i || 0;
        var L= this.length;
        while(i< L){
            if(this[i]=== what) return i;
            ++i;
        }
        return -1;
    }
}
Array.prototype.add= function(){
    var what, a= arguments, i= 0, L= a.length;
    while(i<L){
        what= a[i++];
        if(this.indexOf(what)=== -1) this.push(what);
    }
    return this;
}
Array.prototype.merge= function(target){
    return this.add.apply(target, this);
}

var a1= [1, 2, 3, 4, 5, 6], a2= [2, 4, 6, 8, 10];
a1.merge(a2)
/*  returned value: (Array)
2, 4, 6, 8, 10, 1, 3, 5
*/

答案 2 :(得分:0)

这是函数的简化版本,只在内循环结束后添加过滤器项:

function FilterItems(attrName, attrValue, priceMin, priceMax) {
   for (var i = 0; i < adlet.item.length; i++) {
      if (adlet.item[i][attrName] == attrValue) {
         var currentItem = adlet.item[i];
         var found = false;
        for (var x = 0; x < filteredItems.length; x++) {
           if (filteredItems[x].OMSID == currentItem.OMSID) {
              found = true;
              break;
           }
        }
        if (!found)
           filteredItems.push(currentItem);
      }
  }
}  
相关问题