Comments (4)
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 😄
from deep-object-diff.
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
}
from deep-object-diff.
@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).
from deep-object-diff.
@rbrlortie @williamboman I'm going to close this, let me know if you need anymore help 😄
from deep-object-diff.
Related Issues (20)
- is there any apply method ? HOT 8
- deep-object-merge ? HOT 13
- detailedDiff behavior when deleting items from an array does not match README.md documentation HOT 2
- detailedDiff behavior when the last item in an array is deleted, breaking change between 1.1.0 and 1.1.7 HOT 2
- detailedDiff doesn't return deleted elements HOT 1
- Also return original values for deleted and updated? Feature request. HOT 3
- JSON.stringify will omit deleted keys in the result because they are set to undefined HOT 2
- Arrays are returned as Objects, HOT 2
- Return type for `detailedDiff` HOT 1
- No version specified in mjs and cjs spawns errors in webpack HOT 3
- updatedDiff() not suitable for nested array? HOT 3
- Critical security vulnerability HOT 16
- an option to detect "inserted" and removed items in arrays HOT 2
- hasOwnProperty is not a function HOT 3
- Not clear behaviour of non-ordered arrays comparison with addedDiff
- when object removed out of array in detailedDiff: get as removal not as change of every index
- Feature Request: Add `equals` method
- Arrays returned as objects HOT 3
- Allow some degree of configuration HOT 1
- A better way to test for changes
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deep-object-diff.