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

Deleting Array elements #47

Closed
MrMojoRisin49 opened this issue Jun 26, 2015 · 4 comments
Closed

Deleting Array elements #47

MrMojoRisin49 opened this issue Jun 26, 2015 · 4 comments

Comments

@MrMojoRisin49
Copy link

Not sure but I think I've run into an issue dealing with deleting array elements. For example:

oldArray: [thing1, thing2, thing3, thing4]
newArray: [thing1, thing2]

I apply the diff using this code:

diff.observableDiff(oldArray, newArray, function (d) {
diff.applyChange(oldArray, newArray, d);
});

And I expect:

oldArray: [thing1, thing2]

But I get:

oldArray: [thing1, thing2, thing4]

Not sure why this is. Is this an issue with my code or yours. Any help is greatly appreciated. Thanks in advance.

@jonathan-reisdorf
Copy link

Same issue here. I guess if you just loop through the diff and apply the changes one by one, the former index 3 will be index 2 after the deletion of thing3. The next applyChange then triggers a delete on thing4, which is recorded to be index 3, but now there is no index 3, so nothing happens.

@jonathan-reisdorf
Copy link

For a workaround see here:
#35

@ilovett
Copy link

ilovett commented Nov 10, 2015

I am also seeing this issue where the index seems to be changed and the diff index is no longer accurate... The workaround mentioned by @jonathan-reisdorf in #35 is to call reverse() on your array of changes...

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

The default behaviour of deep-diff should be to provide the diffs of arrays already reversed, going from highest index to lowest.

@postrational
Copy link

+1 Same issue. Can the observableDiff function be changed to apply changes in reversed order in the library?

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

4 participants