GithubHelp home page GithubHelp logo

Comments (4)

mattphillips avatar mattphillips commented on May 22, 2024 1

Hey sorry for the delay in replying @rbrlortie @williamboman - thanks for raising the issue.

The problem around arrays is a tough one because of the context of an index. When diffing an array an item may have been updated, removed or added. The way these differences are represented is by preserving the value as an object with the original indexes for the items that have differences.

For example inserting into the start of an array will cause all entries to have been updated and the last to appear to have been added.

Ideally if it is possible any additional preservation of an array should happen outside of the diff function. deep-object-diff will provide the differences in two data structures and then the consumer can chose to do something with these difference a bit like @williamboman's example.

I hope what I have said makes sense and helps. Let me know if you need any more help 😄

from deep-object-diff.

williamboman avatar williamboman commented on May 22, 2024

Having the same problem.

Here's an (untested, semi-pseudo coded) work-around (will only handle array at root level in the object).

import {diff as _diff} from 'deep-object-diff'

function diff(obj1, obj2) {
    const arrays = Object.keys(obj1).filter(k => Array.isArray(obj1[k]))
    const res = _diff(obj1, obj2)
    Object.keys(res).filter(k => arrays.includes(k)).forEach(k => res[k] = obj2[k])
    return res
}

from deep-object-diff.

williamboman avatar williamboman commented on May 22, 2024

@mattphillips That makes total sense, it's definitely out of scope for this project. I might open-source a solution to this as a separate package in the future (that essentially wraps this module but completely disregards arrays from the diffing algo).

from deep-object-diff.

mattphillips avatar mattphillips commented on May 22, 2024

@rbrlortie @williamboman I'm going to close this, let me know if you need anymore help 😄

from deep-object-diff.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.