How can i compare two collections in backbone?
I have two collection 1C contains 1,2,3
while 2C contains 2,4,5
What i want to do is to remove 2
from 2C because 1C already has a value of 2 after that normally render the collection.
I tried this
this.1C.each(function(model1){
this.2C.each(function(model2){
if(model1 === model2){
2C.remove(model2);
}
});
});
But it doesnt work. Any ideas?
I would hazard a guess that model1 and model2 are never the same instance of a model and therefore never match. Have you tried comparing model ID's or similar? eg. model1.id == model2.id
maybe add some debugging so you can see what's happening..
this.1C.each(function(model1){
console.log('m1:'+model1.categoryCode);
this.2C.each(function(model2){
console.log(' m2:'+model2.categoryCode);
if(model1.categoryCode == model2.categoryCode){
console.log('removing m2:'+model2.categoryCode);
2C.remove(model2);
}
});
});