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

How to get an array of objects with key to show diff? #19

Open
rmtuckerphx opened this issue Apr 13, 2020 · 2 comments
Open

How to get an array of objects with key to show diff? #19

rmtuckerphx opened this issue Apr 13, 2020 · 2 comments

Comments

@rmtuckerphx
Copy link

I have the following code:

var diffJson = require("diff-json")

oldArray = [
  {
    "id": "a",
    "count": 1
  },
  {
    "id": "b",
    "count": 0
  }
];

newArray = [
  {
    "id": "b",
    "count": 1
  },
  {
    "id": "a",
    "count": 3
  }
];

var diffs = diffJson.diff(oldArray, newArray);

And the result is showing as:

[Object {changes: [, ], embededKey: "$index", key: "$root", type: "update"}]

What should the call to .diff be?
What do the other params (path, embededObjKeys, keyPath) mean?

@lveillard
Copy link

If you want that one to work, you will need to sort the arrays, for example like this:
sort((x, y) => (x.id > y.id ? 1 : -1)

@marke-apexit
Copy link

I would also like to compare two arrays by a key (or multi-part key) that I define. I see the suggestion for sorting the arrays, but I don't think that will deal with misalignment due to 'gaps' in the arrays without defining a key.

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