I have a javascript object on my DB, its personal data, this is the structure:
const storedUserData = {
name: "John",
sirname:"Doe",
phone: "666-666-66666",
streetName: "Fake Street",
streetNumber: "123",
zipCode: "90125"
}
and I have a separate object, which has the same structure, which basically is on the front end, and its the result of reading form data.
const formData = {
name: "John",
sirname:"Doe",
phone: "666-666-66666",
streetName: "Fake Street",
streetNumber: "123",
zipCode: "90125"
}
Basically, when the user clicks submit I want to check if there are differences between the stored object storedUserData, above, and the new object, formData. If there are differences, save the differences to the DB.
Of course, I could go on like this for each property, since there are few:
if(storedUserData.name !== formData.name) {
pushDataToDb()
}
but its the lazy approach, and I want to do it correctly. I've been reading into object keys, but I cant figure it out. How could I successfully loop between each property of both items comparing them, and then only if there is a change between the two properties I would push to DB.
Thank you.