deep-object-diff icon indicating copy to clipboard operation
deep-object-diff copied to clipboard

The array detailedDiff does not work

Open mtermoul opened this issue 5 years ago • 1 comments

I am just trying this library for simple arrays and it's not working as I expected. This is the code:

const a1 = [2, 5, 8, 20];
const a2 = [5, 7, 9, 20];
const diff = detailedDiff(a1, a2);
// this is the result
// { added: {},
//   deleted: {},
//  updated: {0: 5, 1: 7, 2: 9} }

// I was expecting this result
// { added: {0: 7, 1: 9}
// deleted: {0: 2, 1: 8}
// updated: {}}

Can someone explain this result?

mtermoul avatar Nov 06 '19 15:11 mtermoul

It's because the keys of your 4-element array are the indexes 0–3, and none of them go away, they just change to contain different values.

In other words, the sequence of operations to get from [2, 5, 8, 20] to [5, 7, 9, 20] is—

a[0] = 5
a[1] = 7
a[2] = 9

—all of which are updates to existing indexes.

anko avatar Nov 12 '19 10:11 anko