开发者

Checking array items against another array

I'm trying to test for a value within an array and act accordingly based on that result i.e., if the item does not exist in the array being tested then add it to the array. I've already spent way too much time on this, I could really use some help.

    function FilterItems(attrName, attrValue, priceMin, priceMax) {
        // check if item exists in filtered items
        for (var i = 0; i < adlet.item.leng开发者_C百科th; 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);
                }
            }
        }


You are adding currentItem in each iteration where it's not found. filteredItems.push(currentItem); must be called after the loop if not found:

...

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

...


/* The array method 'indexOf' is too useful to ignore, if you do anything with arrays. There is a shim here, based on Mozilla's code.

The method 'add' shown here works like push, but it only adds an item if it is not in the array already. You can add multiple items with multiple arguments.

'merge' will add all 'new' items from the calling array to the array sent as the argument, */

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
*/


Here's a simplified version of your function, that only adds the filter item after the inner loop finishes:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜