-
Notifications
You must be signed in to change notification settings - Fork 212
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
Taking diff of two array's not working #35
Comments
See pull request #43, which provides a fix for this. Even after the pull request, however, this will only work if there is only one diff between lhs and rhs. If you need to apply two array diffs, you must be aware that the order in which you apply diffs determines whether the changes can be made. This is because the diff indicates which index to add/remove/edit, but array indexes change when you delete elements. For example, the code below doesn't work as expected: var lhs = ["a","a","a"];
var rhs = ["a"];
var differences = deep.diff(lhs, rhs);
differences.forEach(function (change) {
deep.applyChange(lhs, true, change);
});
// lhs === ["a","a"] A workaround is to apply changes in the reverse order in which they are given var lhs = ["a","a","a"];
var rhs = ["a"];
var differences = deep.diff(lhs, rhs);
for (var i = differences.length - 1; i >= 0; i--) {
deep.applyChange(lhs, true, differences[i]);
}
// lhs === ["a"] |
Great tip. To simplify it, you can also write
But there should be a wrapper method to make that easier anyway. I was hoping I could use |
Oh, I just saw that the @joeldenning has provided is for a different problem (useful for me anyway) |
Fix for issue #35 -- apply diffs for arrays
Verified fixed 2017-04-24. |
Taking the diff like so:
fails when trying to apply that change with this message:
/Users/trich/Sites/deepDiff/index.js:195
, last = change.path.length - 1
^
TypeError: Cannot read property 'length' of undefined
at Function.applyChange (/Users/trich/Sites/deepDiff/index.js:195:27)
at observed (/Users/trich/Sites/deepDiff/examples/example1.js:38:14)
at Array.forEach (native)
at Object. (/Users/trich/Sites/deepDiff/examples/example1.js:37:13)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
The text was updated successfully, but these errors were encountered: