viruschidai / diff-json

A javascript object diff tool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

rmtuckerphx opened this issue · comments

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?

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)

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.