-
Notifications
You must be signed in to change notification settings - Fork 214
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
Work with array of objects #100
Comments
I have the same problem when I delete the first element of an array. It creates n differences for an array of n elements.
result:
I just think it should return [{"kind": "A","index": 0,"item": {"kind": "D","lhs": "1"}] |
I think the solution could be to, instead of always comparing items in both arrays index-by-index, provide an option to pass a function which specifies how to compare items within an array by some kind of unique identifier or hash. Typically this will just be When such a function is supplied, it would be used to detect whether an item in one array exists in the other. Note that if it does and it is an object (or other array), these objects themselves still need to be compared because they might have other changes. When the function is not supplied, the current index-by-index comparison can still be used as fallback. |
@vincentsels I agree with your idea. We need a function to tell the library how to compare objects (e.g. by Is there any workaround yet or do we need to wait for a PR? |
@pschaub I'm using something like this (one level diff) import deepDiff from 'deep-diff';
function arrayCompare(arrFirst, arrSecond, propName) {
const arrDiff = [];
let fields;
let obj;
for (let key2 in arrSecond) {
obj = arrFirst.filter(o => o[propName] === arrSecond[key2][propName]);
obj = obj[0];
if (obj !== undefined) {
fields = deepDiff(obj, arrSecond[key2]);
}
if (fields !== undefined) {
arrDiff.push({ key: key2, fields });
}
}
if (arrDiff.length) {
return arrDiff;
}
return false;
}
const diff = arrayCompare(firstArr, secondArr, 'id'); |
btw: this issue is a duplicate, see #37 |
@NewOldMax But your solution will not work with nesting (e.g. array > object > array > object > ..), right? |
Didn't test with nested, in my cases I have only first-level differences |
@NewOldMax I don't see any kind of recursion in your code so this shouldn't work in nested situations. We really need a fix inside the library itself. But thanks for your workaround for 1st-level-differences. |
any progress? |
@kenberkeley We switched to https://github.com/benjamine/jsondiffpatch to solve our nested situation. Maybe this will help you until anybody will implement a way into this package. |
Any updates in regards to this issue. |
Hi,
In my case, I have some array of objects ordered by
name
key.This code
says that I have 6 changes, but in real only 1 object is changed
{id: 1, name: "e"}
. Is there a way to get something like "1 edition in{id: 1, name: "e"}
"?I want to use it for a making new items for user, like "hey, this item has been changed". Can I achieve this with your library?
The text was updated successfully, but these errors were encountered: