0

I would like to compare two objects: I try to use this function:

export const isArrayEqual = (x, y) => {
  console.log('x',x);
  return _(x).xorWith(y, _.isEqual).isEmpty();
};

But i think that is not enought deep to check all the properties of the object. How can i do it, deepless and check all the properties with the same function?

obj1:

{
        "list": {
            "text_list": "RESPUESTA CON LSITA 2",
            "items_list": [
                {
                    "value": "Valor 2",
                    "synonyms": [],
                    "idResponse": "CA2_1_E_2",
                    "title_item": "RESPUESTA 2"
                },
                {
                    "value": "Valor 3",
                    "synonyms": [],
                    "idResponse": "CA2_1_E_3",
                    "title_item": "Respuesta 3"
                }
            ],
            "title_list": "Titulo en EDICION"
        },
        "lateral": {},
        "finalResponse": {}
    }

obj2:

{
        "list": {
            "text_list": "RESPUESTA Editada",
            "items_list": [
                {
                    "value": "Valor 2",
                    "synonyms": [],
                    "idResponse": "CA2_1_E_2",
                    "title_item": "RESPUESTA 2"
                },
                {
                    "value": "Valor 3",
                    "synonyms": [],
                    "idResponse": "CA2_1_E_3",
                    "title_item": "Respuesta 5"
                }
            ],
            "title_list": "Titulo en EDICION"
        },
        "lateral": {},
        "finalResponse": {}
    }
ddaudiosolutions
  • 131
  • 2
  • 15
  • 3
    According to [this question](https://stackoverflow.com/questions/31683075/how-to-do-a-deep-comparison-between-2-objects-with-lodash) it does a deep comparison. Why do you think it doesn't? – Barmar Mar 29 '23 at 16:38
  • 1
    What's the problem you're trying to solve? As for isEqual() function, you can see it works perfectly in this sandbox: https://codesandbox.io/s/epic-matan-qksuqf?file=/src/index.js If you're trying to compare two arrays this function should do the trick by itself. – danielperaza Mar 29 '23 at 16:44
  • I don't know why, i will try again. – ddaudiosolutions Mar 29 '23 at 17:32
  • @Barmar my first option isArrayEqual = (x, y), not works, the solution from _danielperaza is better. I don't know why. https://codesandbox.io/s/isequal-vs-worth-f95lrr?file=/src/index.js here both options. – ddaudiosolutions Mar 30 '23 at 06:33
  • When `.xorWith().isEmpty()` returns `true`, it means the objects are equal. So what's the problem? – Barmar Mar 30 '23 at 14:31

1 Answers1

2

Lodash's isEqual() function should suffice as shown in this sandbox https://codesandbox.io/s/epic-matan-qksuqf?file=/src/index.js.

If you're still not satisfied with this, you can implement your own function using recursion.

danielperaza
  • 412
  • 3
  • 12