Take only what was changed on an object

Asked

Viewed 89 times

2

I implemented a PATCH function to update the data of a resource, but I need to only take the updated data of an already loaded object on the screen to send in the request.

What would be the most efficient way to do this? You can use the $watch or better avoid?

1 answer

1


It was much simpler than I thought. I just created a function that compares two objects and returns an object with the changes.

function diffBetween(old, cur) {
    var updated = {};
    for (var prop in cur) {
        if (old[prop] != cur[prop]) {
            updated[prop] = cur[prop];
        }
    }
    return updated;
}

Browser other questions tagged

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