I have a flat JavaScript object called dictionary
that I want to update based on other flat objects. Sometimes I want to add every property of an object to dictionary
and sometimes I want to delete every key in dictionary
that exists in another object. I'll be using the values of those properties later, but for the sake of managing dictionary
, I only care about their names.
From this question, I know how to merge properties (using, for instance, Object.assign
). How can I do the inverse of that? Object.unassign
doesn't exist, but is there anything similar?
For example:
dictionary = { foo: 'bar', baz: 'qux' };
object1 = { spam: 'spam' };
Object.assign(dictionary, object1); // dictionary now has properties: foo, baz, spam
object2 = { foo: 'spam', baz: 'qux' };
Object.unassign(dictionary, object2); // dictionary is now only { spam: 'spam' }