Skip to content

Array order considered as not equal? #100

@trainoasis

Description

@trainoasis

Hey, thanks for the code. I'm trying to replace our custom implementation with this lib and I noticed that

filter1: {
    something: {
        values: ["Item-1", "Item-2"],
        operator: 1,
        extra: { data: "something", another: "xyz" },
    }
},
filter2: {
     something: {
        extra: { data: "something", another: "xyz" },
        operator: 1,
        values: ["Item-2", "Item-1"],
    },
}

these two are not considered equal when using diff() dye to array order in "values". detailedDiff confirms that:

Object {
       "added": Object {},
       "deleted": Object {},
       "updated":{"something":{"values":{"0":"Item-2","1":"Item-1"}}},
}

Is this expected? I didn't see any options to ignore the order in arrays for example, but that could probably be added, right?

I can close this if this is expected and won't be addressed.

PS: what I currently do to go around this is order all arrays within the object which is obviously adding yet another loop that's otherwise already happening in your code - but this could probably be simply added for each array in your codebase. LMK if you want me to make a PR.

detailedDiff(
  sortObjectArrays(filter1),
  sortObjectArrays(filter2)
)

Another thing I noticed is that in React, having undefined as value is problematic as the renderer just assumes empty object and removes keys with values undefined.

{
   added: {},
   deleted: {
       someProp: undefined,
   },
   updated: {},
}

this becomes just

{
   added: {},
   deleted: {},
   updated: {},
}

which effectively loses it's value unfortunately. Perhaps keys should be listed in an array that show what was removed?

Activity

joecotton-softworks

joecotton-softworks commented on Feb 19, 2025

@joecotton-softworks

A little late to the party 😁

I believe this is intentional. The reason that two arrays with the same elements in different orders aren't considered equal is because arrays are generally considered to be ordered collections, not unordered. For instance, the JSON specification considers an array to be an ordered list (link), which means that the arrays you provided above wouldn't be considered equivalent.

(Yes, I realize that JavaScript and JSON aren't technically the same thing, but you get my point).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @trainoasis@joecotton-softworks

        Issue actions

          Array order considered as not equal? · Issue #100 · mattphillips/deep-object-diff