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

Array diff when deleting first item #298

Open
Benwick91 opened this issue Sep 10, 2020 · 2 comments
Open

Array diff when deleting first item #298

Benwick91 opened this issue Sep 10, 2020 · 2 comments

Comments

@Benwick91
Copy link

Benwick91 commented Sep 10, 2020

Hey

I want to use the diff and patch functions for an undo function in my React-Redux project. It works fine, when I am deleting the last item, but not when I deleting the first one or an item inside.

For example I delete the first item:

{"state":[
  {"x": 1, "y": 3},
  {"x": 4, "y": 5},
  {"x": 2, "y": 6},
  {"x": 0, "y": 9}
 ]
}

{"state":[
  {"x": 4, "y": 5},
  {"x": 2, "y": 6},
  {"x": 0, "y": 9}
 ]
}

The result of diff is:

{
  "state": {
    "0": {
      "x": [
        1,
        4
      ],
      "y": [
        3,
        5
      ]
    },
    "1": {
      "x": [
        4,
        2
      ],
      "y": [
        5,
        6
      ]
    },
    "2": {
      "x": [
        2,
        0
      ],
      "y": [
        6,
        9
      ]
    },
    "_t": "a",
    "_3": [
      {
        "x": 0,
        "y": 9
      },
      0,
      0
    ]
  }
}

But I want to get:

{
  "state": {
    "0": [
      {
        "x": 1,
        "y": 3
      },
      0,
      0
    ]
  }
}

Is this possible?
Thank you :)

@vedmalex
Copy link

it is mentioned in documentation that you need to use object has function

@DaTebe
Copy link

DaTebe commented Apr 10, 2021

As mentioned in #302 I use something like this (with lodash and hasha as dependency):

objectHash: function(obj) {
        // this function is used only to when objects are not equal by ref
        const newObj = _(obj).toPairs().sortBy(0).fromPairs().value();
        const hash = hasha(JSON.stringify(newObj), {algorithm: "sha256"})
        return hash;
    }

Maybe it fits you?

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