I try to find the most effective way to compare two objects of objects and return filtered difference in JavaScript.
I build an app to update stock in DB. But this app can use many users in the same time. I would like to avoid calling API and refresh all data in app each time changes are made, bcz database is too big and loading time is long. I have and old data locally, with help of MQTT I receive new data the moment its updated by someone. The goal is to find the difference between two objects and update locally only the one that has changes. Objects looks like this:
let oldData = [
{ artnr: 12, affiliateid: 1, stock: 10 },
{ artnr: 12, affiliateid: 2, stock: 15 },
{ artnr: 12, affiliateid: 3, stock: 1 },
{ artnr: 13, affiliateid: 2, stock: 2 }
]
let newData = [
{ artnr: 12, affiliateid: 1, stock: 11 },
{ artnr: 12, affiliateid: 2, stock: 20 },
{ artnr: 12, affiliateid: 3, stock: 1 },
{ artnr: 13, affiliateid: 2, stock: 2 }
]
The only way I can think about it’s iterations. But in real life each of objects will contain thousands of subObjects, so I would like to avoid iterations type of
var diff = []
for (var i = 0; i < oldData.length; i++) {
// compare sub objects here
diff = [diff, ... {updated obj}]
}
return diff
Is there an effective way to white func
const FilterObjectDiff = (oldData, newData) => {
// filter here without iterations
return [
{ artnr: 12, affiliateid: 1, stock: 11 },
{ artnr: 12, affiliateid: 2, stock: 20 }
]
}
I would appreciate any ideas.