Checking for equalities between objects

Asked

Viewed 36 times

2

Well, I need to check the content of 2 objects, I need to know if they are the same or not in the main fields.

My idea is to check by inserting some data in the localStorage if that data already exists, if it exists I should replace the existing object in the localStorage array, if not I need to create.

Example:

Object1 {TXT_NOMEX_PESSO: "Renan Rodrigues Moraes", COD_IDENT_PESSO: "9999160307115906859", FLG_IDENT_PESSO: "L", FLG_STATU_PESSO: "A", FLG_STATU_PARTC: "A"…}

Object2 {TXT_NOMEX_PESSO: "Lidiane Morais", COD_IDENT_PESSO: "9999160307134108952", FLG_IDENT_PESSO: "L", FLG_STATU_PESSO: "A", FLG_STATU_PARTC: "A"…}

Object3 {TXT_NOMEX_PESSO: "Renan Rodrigues", COD_IDENT_PESSO: "9999160307115906859", FLG_IDENT_PESSO: "L", FLG_STATU_PESSO: "A", FLG_STATU_PARTC: "A"…}

Should I enter the Object1 and right after the Object2 it would insert normal because there would be no why, in case I inserted the Object3he should replace the Object1 for Object3.

I need to do this as simply as possible, if there is one. The method I know to do this, is to go through every tuple of chave : valor and check whether it is the same.

I forgot to report something very important, as this is all for localStorage I need to know if it exists or not, I mean, I’m not always sure it will have 2 identical objects.

  • Wouldn’t it be easier for you to save these objects with a single key and then get and replace them without having to compare all the properties? So you would have to just compare the unique key of the object. This applies to your case?

  • @Ulyssesalves my idea is to develop something to facilitate the use of localStorage then, the idea is that for example person table the key of it for example in localStorage will be pes_ plus the person’s code. This to be sure that if I am going to change a data and give some problem I do not lose others.

  • In my opinion, it would be better then to create an object to represent each "table" on the client side. Type instead of the prefix pes, I would create an object Pessoas, or a vector, and within it would save all human objects. It would be easier to access the different objects later, as a repository pattern. Still, each individual object would need a unique code, in which case the prefix pes_ added with the person code would already solve the problem in that case.

  • So that’s exactly what I do, but I can’t put the person key and the value an array JSON with all the information, for the reason located, for example I want to edit the middle guy of the array, but gives an error in saving, with this I would lose the data of all people. You understand my concern ?

  • 1

    I get it. Really, in this case for you to have security even if your code will survive errors is implementing a small repository library. Hence your repository’s CRUD methods have to use Try/catch to know if the command worked or not. Within this library you would also have to implement the collection and persistence of objects in a unique way... Or we can find a library that already does this, and then you just use it in your project.

  • 1

    Here is a page listing 9 ready-to-use libraries for localstorage: https://www.sitepoint.com/9-javascript-libraries-working-with-local-storage/

  • 1

    More yet @Ulyssesalves helped me in my doubts. I will continue in the project, because I think what I am developing is something different, and will facilitate some people in the use of localStorage I am using commands and strategies to make sure that my code will not fail, and even because I myself am being a user of it in my projects. Thanks for the help.

Show 2 more comments

2 answers

1

The easiest way is to transform each of the objects into strings and compare them:

JSON.stringify(obj1) === JSON.stringify(obj2);

Source: https://stackoverflow.com/questions/13142968/deep-comparison-of-objects-arrays

EDIT: how the problem involves LocalStorage, I suggest the following solution:

function saoIguais(nomeObj1, nomeObj2) {
    var obj1 = localStorage.getItem(nomeObj1)
    var obj2 = localStorage.getItem(nomeObj2)
    return obj1 !== null && obj2 !== null && JSON.stringify(obj1) === JSON.stringify(obj2)
}

1


You can iterate all fields of the first object and check if the value of these fields matches at the same value as the field in the second object.

function compareObjetos(objA, objB) {
    if (objA == null || objB == null) {
        // Se um dos objetos é nulo então são objetos diferentes. 
        // Mesmo por que null nem mesmo é um objeto...
        return false;
    }

    for (prop in objA) {
        if(!(objB[prop] === objA[prop])) {
            // Os objetos são diferentes.
            return false;
        }
    }

    // Os objetos são equivalentes.
    return true;
}
  • Something important, that I forgot to inform, is that I do not know if there is or not that object in the localStorage

  • @Renanrodrigues in this case, the method can disregard null objects and indicate them as different. I edited the answer to consider this case where one or both objects are null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.