Let’s say I have a JavaScript object that is made up of several key/value pairs of string keys and JavaScript objects.
var obList = { key:{..}, key2:{..}, key3:{..}, ... }
And I construct a new set, obList2
var obList2 = { key:{..}, key2:{..}, key3:{..}, ... }
I want to write a function that modifies obList based on obList2. The initial step I’m having trouble with is removing all objects in obList that have a key not present in obList2. Any thoughts/implementation of this would be v helpful. Thank you!
You can loop over objects using a
for .. inloop and check if properties exist usingObject.hasOwnProperty().Example:
The first condition is a safeguard against modifications to
Object.prototypeand the second checks to see if the property isn’t present on the second object.