Combining two objects and removing properties that are missing from the original

I have an external application where a user can add / remove fields from their profile. I'm trying to make a PUT API request and usually I just _.merge (original, req.body), but the updated object has multiple properties removed from the object that is not on the original object.

Is there a way to combine the objects and remove the fields that were removed on the new object are also removed from the original - while keeping the original object and concatenating the values ​​that are present on both objects (this is a mongoose model with identity properties and what I need to keep)

Thank!

+3


source to share


1 answer


Use defaults

or plus defaultsDeep

herepick

.

For example:



const original = {check: 1};
const anotherObj = {check: 3, data: 2};
const resultMerge = _.defaults(anotherObj, original);
const result = _.pick(resultMerge, _.keys(original));

// result => {check: 3};

      

+3


source







All Articles