Reputation: 7601
I have two lists, oldPanodatas
and newPanodatas
. I want to get only the objects in newPanodatas
that aren't present in oldPanodatas
. I did this:
var filteredPanodatas = _.difference(newPanodatas, oldPanodatas)
But I'm getting all the items, _.difference
is not filtering anything whatsoever:
OLD: Object {roomModelId: "56a9e0088ac247005538d6d3", x: 262, index: 1, y: 211, panoDataRotate: 0…}
OLD: Object {roomModelId: "56a9e0088ac247005538d6d3", x: 177, index: 0, y: 182, panoDataRotate: 0…}
NEW: Object {index: 0, x: 177, y: 182, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}
NEW: Object {index: 1, x: 262, y: 211, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}
NEW: Object {index: 2, x: 200, y: 200, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}
FILTERED: Object {index: 0, x: 177, y: 182, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}
FILTERED: Object {index: 1, x: 262, y: 211, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}
FILTERED: Object {index: 2, x: 200, y: 200, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}
Why is this? What's the correct way to accomplish what I want?
Upvotes: 0
Views: 41
Reputation: 48406
Try it with differceWith
, the isEqual
comparator which is invoked to compare elements of array to values
_.differenceWith(newPanodatas, oldPanodatas, _.isEqual);
Code sample,
var old = [{roomModelId: "56a9e0088ac247005538d6d3", x: 262, index: 1, y: 211, panoDataRotate: 0}, {roomModelId: "56a9e0088ac247005538d6d3", x: 177, index: 0, y: 182, panoDataRotate: 0}];
newobj = [{index: 0, x: 177, y: 182, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}, {index: 1, x: 262, y: 211, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}, {index: 2, x: 200, y: 200, roomModelId: "56a9e0088ac247005538d6d3", panoDataRotate: 0}];
r = _.differenceWith(newobj, old, _.isEqual);
The result of r
[{index: 2, panoDataRotate: 0, roomModelId: "56a9e0088ac247005538d6d3", x: 200, y: 200}]
Upvotes: 1