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);
在上面的代码中我想删除myFind_collections中与pgRangeCollection相等的所有项目
我希望输出为(3,6,10) 但我得到的输出为(248,249,250)
我不知道我错误的地方可以为任何人提出解决方案,
提前致谢
答案 0 :(得分:5)
使用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 ]
答案 1 :(得分:0)
答案应该是使用KooiInc已经提出的.filter方法,但由于IE是一个要求,我会使用Lodash或Underscore的差异方法:
_.difference(myFind_collections, pgRangeCollection) // [3,6,10]
只需通过cdn包含它: http://cdnjs.com/libraries/lodash.js/
或者你已经有了jQuery:
$(myFind_collections).not(pgRangeCollection).get() // [3,6,10]