gpt4 book ai didi

javascript - Lodash差异通过澄清

转载 作者:行者123 更新时间:2023-12-03 13:45:03 24 4
gpt4 key购买 nike

我有两个对象数组,我想通过 identifier 找到 newData 和 oldData 数组之间的区别,显示差异,其中 oldData 的标识符与 newData 数组不同,这是我的数组:

const newData = [
{
"extras": {},
"identifier": "13",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
},
{
"extras": {},
"identifier": "18",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
}

]

const oldData = [
{
"identifier": "13",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
},
{
"identifier": "12",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
}
]

这是我正在做的事情:

let testDifference = _.differenceBy(newData, oldData, "identifier")

我的期望,我会得到

[
{
"identifier": "12",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
},
]

实际上,我得到的是空数组,我做错了什么?如何使其正常工作?

最佳答案

我认为您只需将前两个参数切换为 differenceBy 即可获得您期望的结果。您可以将其视为除第二个数组项之外的第一个数组项。

const newData = [{
"extras": {},
"identifier": "13",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
}]

const oldData = [{
"identifier": "13",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
},
{
"identifier": "12",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
}
];

let whatDelete = _.differenceBy(oldData, newData, "identifier");

console.log(whatDelete);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

关于javascript - Lodash差异通过澄清,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57888023/

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