0
var myFind_collections =[3,6,10,234,235,236,237,238,239,240,241,244,245,246,247,248,248,249,250];
var pgRangeCollection = [234,235,236,237,238,239,240,241,244,245,246,247,248,248,249,250];

for(v=0;v<=pgRangeCollection.length;v++){
    var pgMatch = pgRangeCollection[v];
    clear_pg_range(pgMatch);
    }

function clear_pg_range(pgMatch){
        //for(d=0;d<=myFind_collections.length-1;d++){
        for(d=0;d<=myFind_collections.length-1;d++){
            var docFound = parseInt(myFind_collections[d]);
            if(pgMatch===docFound){
                    myFind_collections.splice(myFind_collections[d],1);
                    alert(docFound + " was removed");
                }
            }
    }

alert(myFind_collections.length);

in above code i want to remove every item in myFind_collections which equals to pgRangeCollection

i want the output as (3,6,10) but i am getting the output as (248,249,250)

i dont know where i mistaking can anybody suggest solution for this,

Thanks in advance

user3354853
  • 139
  • 8
  • See if this helps https://github.com/jashkenas/underscore/blob/master/underscore.js#L518-L523 – elclanrs Feb 26 '14 at 08:16
  • There is smth like you are asking on SO already http://stackoverflow.com/questions/3954438/remove-item-from-array-by-value – Y.Puzyrenko Feb 26 '14 at 08:16
  • You might find it handy to use a set and use the difference method on the set: https://github.com/jfriend00/Javascript-Set/blob/master/set.js. Arrays aren't very efficient at doing lookups like you're doing here. That's what sets (implemented in JS with an object and properties on the object) are for. – jfriend00 Feb 26 '14 at 08:25

2 Answers2

5

Use Array.filter

var myFind_collections =[3,6,10,234,235,236,237,238,239,240,241,244,245,246,247,248,248,249,250];
var pgRangeCollection = [234,235,236,237,238,239,240,241,244,245,246,247,248,248,249,250];
var filtered = myFind_collections.filter(
                  function(a){return pgRangeCollection.indexOf(a) < 0}
               ); // => [ 3, 6, 10 ]

See also (filter) and also (indexOf)

KooiInc
  • 104,388
  • 28
  • 131
  • 164
0

The answer should be using the .filter method as already proposed by KooiInc but since IE is a requirement I would use the difference method of Lodash or Underscore:

_.difference(myFind_collections, pgRangeCollection) // [3,6,10]

Just include it via cdn: http://cdnjs.com/libraries/lodash.js/

or if you already have jQuery:

 $(myFind_collections).not(pgRangeCollection).get() // [3,6,10]
Christian Landgren
  • 11,124
  • 6
  • 31
  • 31