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 Object3
he 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?
– Ulysses Alves
@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 inlocalStorage
will bepes_
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.– Renan Rodrigues
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 objectPessoas
, 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 prefixpes_
added with the person code would already solve the problem in that case.– Ulysses Alves
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 ?– Renan Rodrigues
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.
– Ulysses Alves
Here is a page listing 9 ready-to-use libraries for localstorage: https://www.sitepoint.com/9-javascript-libraries-working-with-local-storage/
– Ulysses Alves
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.– Renan Rodrigues