I'm using underscore.js on my project and i want to compare two arrays but only their intersection by one field.
The first array :
[{item: myObject1, label: myObject1.name, ticked: true, disabled: false}]
the second array :
[{item: myObject1, label: myObject1.name, ticked: false, disabled: false},
{item: myObject2, label: myObject2.name, ticked: true, disabled: false}]
I want to return intersection of these arrays by myObject.id and compare it :
intersection :
{item: myObject1, label: myObject1.name, ticked: true, disabled: false}
and
{item: myObject1, label: myObject1.name, ticked: false, disabled: false}
because myObject1 is the intersection (myObject1.id == myObject1.id).
- these items are not equals because the first element is ticked and the second no... so, return false;
I don't see how can i do that with underscore.js.
EDIT:
with only isEqual function, i can do that (i use AngularJS):
isIOEquals: function(inputs, outputs){
var editedInputs = [];
var editedOutputs = [];
angular.forEach(outputs, function(outputItem){
angular.forEach(inputs, function(inputItem){
if(_.isEqual(inputItem.item.id, outputItem.item.id)){
editedInputs.push(inputItem);
editedOutputs.push(outputItem);
}
});
});
return _.isEqual(editedInputs, editedOutputs);
}