为什么 _.difference 返回所有项目

Why is _.difference returning all the items?

本文关键字:项目 返回 difference 为什么      更新时间:2023-09-26

我有两个列表,oldPanodatasnewPanodatas。我只想获取newPanodatas中不存在的对象oldPanodatas.我这样做了:

var filteredPanodatas = _.difference(newPanodatas, oldPanodatas)

但是我得到了所有项目,_.difference没有过滤任何内容:

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}

这是为什么呢?实现我想要的目标的正确方法是什么?

尝试使用 differceWithisEqual比较器,用于将数组元素与值进行比较

_.differenceWith(newPanodatas, oldPanodatas, _.isEqual);

代码示例,

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);

r的结果

[{index: 2, panoDataRotate: 0, roomModelId: "56a9e0088ac247005538d6d3", x: 200, y: 200}]