I have a simple object like this:
var obj = {
"option1": "item1",
"option2": "item2",
"option3": "item3"
};
For adding a new property to the object I'm using the following code:
obj[this.value] = this.innerHTML;
// this.innerHTML is used just because I'm adding the value I get from a DOM element
Is there a function that can help me remove a property from the object, that receives as a parameter the value of the key-value pair?
For example removeItem('item3');
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With