Closed rbrlortie closed 7 years ago
Having the same problem.
Here's an (untested, semi-pseudo coded) work-around (will only handle array at root level in the object).
import {diff as _diff} from 'deep-object-diff'
function diff(obj1, obj2) {
const arrays = Object.keys(obj1).filter(k => Array.isArray(obj1[k]))
const res = _diff(obj1, obj2)
Object.keys(res).filter(k => arrays.includes(k)).forEach(k => res[k] = obj2[k])
return res
}
Hey sorry for the delay in replying @rbrlortie @williamboman - thanks for raising the issue.
The problem around arrays is a tough one because of the context of an index. When diffing an array an item may have been updated, removed or added. The way these differences are represented is by preserving the value as an object with the original indexes for the items that have differences.
For example inserting into the start of an array will cause all entries to have been updated and the last to appear to have been added.
Ideally if it is possible any additional preservation of an array should happen outside of the diff function. deep-object-diff
will provide the differences in two data structures and then the consumer can chose to do something with these difference a bit like @williamboman's example.
I hope what I have said makes sense and helps. Let me know if you need any more help :smile:
@mattphillips That makes total sense, it's definitely out of scope for this project. I might open-source a solution to this as a separate package in the future (that essentially wraps this module but completely disregards arrays from the diffing algo).
@rbrlortie @williamboman I'm going to close this, let me know if you need anymore help :smile:
When running deep-object-diff on an object which contains arrays those arrays are turned into objects.
Object on which deep-object-diff is used:
Those values are changed to : QtyOrdered: "7" Total: "9.52" Price: "1.36"
Result of deep-object-diff:
This creates issues where we expect some data to be arrays and they are now objects.