javascript - How to get the difference between to Arrays of Objects without IDs -
similar this question , this question, have array of objects want compare , difference between these two. sadly, objects not contain id (and @ state need call function, can't give id objects.).the examples linked above rely on using objects id comparison, can't use these solutions. arrays this:
var array1 = [{person1id: 46, person2id: 47, value: "test"}, {person1id: 38, person2id: 56, value: "test2"}]; var array2 = [{person1id: 46, person2id: 47, value: "test"}];
in example, object {person1id: 38, person2id: 56, value: "test2"}
, in array1 not in array2.
is there way can compare 2 arrays , difference if objects in array not have id?
you use nested approach check every object of first array every object of second array, property wise.
function getdifference(array1, array2) { var keys = ['person1id', 'person2id', 'value']; return array1.filter(function (a) { return !array2.some(function (b) { return keys.every(function (k) { return a[k] === b[k]; }); }); }); } console.log(getdifference( [{person1id: 46, person2id: 47, value: "test"}, {person1id: 38, person2id: 56, value: "test2"}], [{person1id: 46, person2id: 47, value: "test"}] ));
Comments
Post a Comment