-
-
Notifications
You must be signed in to change notification settings - Fork 96
Arrays are being transformed into objects #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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. I hope what I have said makes sense and helps. Let me know if you need any more help 😄 |
@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 😄 |
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.
The text was updated successfully, but these errors were encountered: