recursive-diff icon indicating copy to clipboard operation
recursive-diff copied to clipboard

applyDiff doesn't work for arrays in objects

Open NagendraSIB opened this issue 1 year ago • 1 comments

initial = { array: [1, 2, 3] };
final = { array: [1, 2, 4] };
const diff: ANY_OBJECT = {};
const diffPath = rdiff.getDiff(initial, final);
rdiff.applyDiff(diff, diffPath);

throwing error on rdiff.applyDiff(diff, diffPath);-

Invalid path: "array,2" for object: {}

NagendraSIB avatar May 08 '24 04:05 NagendraSIB

Hi i think you are using apply diff wrong. The documentation says: applyDiff will apply the diff on the oldVal and update it as per the diff object.

So the a correct usage would be:

initial = { array: [1, 2, 3] };
final = { array: [1, 2, 4] };
const diff = rdiff.getDiff(initial, final);
rdiff.applyDiff(inital, diff);

After that, inital is deeply equal to final. I am not sure why you are creating a new empty obiect. Maybe you are trying something different? In that case, some more information, about what you are trying to achieve would be nice.

michastubi avatar Oct 06 '24 09:10 michastubi