I am managing two distinct collections:
Collection
X
includeselement1
,element2
,element3
,element4
.Collection
Y
consists ofelement2
,element3
.
For illustration purposes:
var element1 = new models.ExModel({id: "1", name: "element1"});
var element2 = new models.ExModel({id: "2", name: "element2"});
var element3 = new models.ExModel({id: "3", name: "element3"});
var element4 = new models.ExModel({id: "4", name: "element4"});
var X = new collections.ExCol([ element1, element2, element3, element4 ]);
var Y = new collections.ExCol([ element2, element3 ]);
I am seeking a way to retrieve an array of models from collection X
that are not present in collection Y
. To provide an example, I would like to obtain an array containing element1
and element4
. What is the most efficient method to achieve this?
I have considered two possible options for accomplishing this task, however, I am uncertain as to which approach is superior. I believe Option1 exhibits greater efficiency compared to Option2.
Option1:
var idsY = Y.pluck("id");
var result = X.filter( function(m) { return idsY.indexOf(m.id) === -1; } );
Option2:
var result = X.filter( function(m) { return !Y.contains(m); });
View the complete example: http://jsfiddle.net/VH3HU/
Thank you!