Skip to content
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

Closed
tnrich opened this issue Jan 13, 2015 · 4 comments
Closed

Taking diff of two array's not working #35

tnrich opened this issue Jan 13, 2015 · 4 comments

Comments

@tnrich
Copy link

tnrich commented Jan 13, 2015

Taking the diff like so:

var lhs = ["a","a"];
var rhs = ["a"];
var differences = deep.diff(lhs, rhs);
differences.forEach(function (change) {
        deep.applyChange(lhs, true, change);
});

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)

@joeldenning
Copy link

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"]

@jonathan-reisdorf
Copy link

Great tip. To simplify it, you can also write

deep.diff(lhs, rhs).reverse().forEach(function(diffItem) {
  deep.applyChange(lhs, true, diffItem);
});

But there should be a wrapper method to make that easier anyway. I was hoping I could use applyDifffor that matter, but it doesn't accept what I had in mind. (I am just transferring the diff result from one browser window to the other in order to get incremental state persistency)

@jonathan-reisdorf
Copy link

Oh, I just saw that the @joeldenning has provided is for a different problem (useful for me anyway)

flitbit pushed a commit that referenced this issue Oct 18, 2015
Fix for issue #35 -- apply diffs for arrays
flitbit pushed a commit that referenced this issue Apr 25, 2017
@flitbit
Copy link
Collaborator

flitbit commented Apr 25, 2017

Verified fixed 2017-04-24.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants