gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:49:11 25 4
gpt4 key购买 nike

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

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}]

关于javascript - 为什么 _.difference 返回所有项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35076025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com