0

I have seen multiple other questions about how to do this, but I am yet to find one that accounts for this scenario.

I have an object (lets call it obj), which has object properties (lets call them objProp). I need to find the key of an objProp so I can delete it from obj. I run into an issue because I cannot use indexOf or anything like this because my values are objects themselves.

//obj declaration etc 

delete obj[getKeyByValue(obj, objProp)];

function getKeyByValue(object, value) {
  return Object.keys(object).find(key => object[key] === value);
};

the above will not work because objProp is another object, is there another way this can be done?

Joe Moore
  • 2,031
  • 2
  • 8
  • 29

1 Answers1

1

According to ECMAScript Specification === will always return false when you compare 2 different objects.

If the order of your objects'(and all inside nested objects) key are the same, JSON.stringify(object[key]) === JSON.stringify(value) may work.

Otherwise, consider something like Lodash's _.isEqual. Either use it directly by introducing lodash as a dependency or implement something similar by your own.

sam
  • 1,767
  • 12
  • 15