I have two arrays of objects, I want to find the difference between newData and oldData arrays by identifier, show difference, where identifiers of oldData are different from newData array, here are my arrays:
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,
}
]
Here what I'm doing:
let testDifference = _.differenceBy(newData, oldData, "identifier")
My expectation, that I will get
[
{
"identifier": "12",
"latitude": 39.13063,
"loiteringDelay": 1000,
"longitude": -86.58286,
"notifyOnDwell": false,
"notifyOnEntry": true,
"notifyOnExit": true,
"radius": 30,
},
]
in reality, I'm getting empty array, what I'm doing wrong? How to make it work correctly?
I think you just need to switch around the first two parameters to differenceBy to get the result you're expecting. You can think of it as the first array items except the second array items.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With