ES6 has a lot of functions including assign and others. But is there a method to get a list of properties that are different from one object to the next?
For example, if I have a component with two states. The default state has 100 properties that define it. State two there are only 10 properties that change. Let's say I get 2 objects containing all 100 properties. I want to create object 3 that has only the 10 properties that have changed (actually not only the properties that changed but the properties on the second object - see update).
The second object keeps all its unique properties and overrides the properties in the first.
I thought Object.assign()
might do this but I don't think so.
var object = {name:Fred, age: 20, weight: 100};
var object2 = {name:Fred, age: 21, weight: 120};
function getChangesFromObjectTwo(object1, object2) {
return object;
}
// returns {age:21, weight: 120};
var changes = getChangesFromObjectTwo(object, object2);
UPDATE:
Great answers. I wasn't specific enough... If object2 has additional properties they should show on the returned object.
var object = {name:Fred, age: 20, weight: 100};
var object2 = {name:Fred, age: 21, weight: 120, height: 70};
function getChangesFromObjectTwo(object1, object2) {
return object;
}
// returns {age:21, weight: 120, height: 70};
var changes = getChangesFromObjectTwo(object, object2);