Remove property by value from object
That would probably be delete
:
delete obj['option1'];
FIDDLE
To do it by value, you'd do something like :
function deleteByVal(val) {
for (var key in obj) {
if (obj[key] == val) delete obj[key];
}
}
deleteByVal('item1');
FIDDLE
This question has been answered correctly but there is just one thing I want to add:
You should write a pure function for this solution. That is a function that does not require your object to be defined in the global scope.
so:
const deleteObjectItemByValue = (Obj, val) => {
for (var key in Obj) {
if (Obj[key] == val) {
delete Obj[key];
return Obj;
}
}
};
usage: deleteObjectItemByValue(yourObj, val);
This is a pure function that does not have side effects i.e mutate anything in its outer environment and does not depend on any global variable for its operation.