Take, for instance, the following object:
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
I know I can use delete fruits["red"]
to remove it by the key name, but how could I delete the object item by the fruit name?
To remove an item from a given array by value, you need to get the index of that value by using the indexOf() function and then use the splice() function to remove the value from the array using its index.
The only way to fully remove the properties of an object in JavaScript is by using delete operator. If the property which you're trying to delete doesn't exist, delete won't have any effect and can return true.
Use a for..in loop to clear an object and delete all its properties. The loop will iterate over all the enumerable properties in the object. On each iteration, use the delete operator to delete the current property. Copied!
To remove a property from all objects in an array: Use the Array. forEach() method to iterate over the array. On each iteration, use the delete operator to delete the specific property. The property will get removed from all objects in the array.
Have you tried something like this?
function deleteByValue(val) {
for(var f in fruits) {
if(fruits[f] == val) {
delete fruits[f];
}
}
}
And as per Rocket's comment, you might want to check hasOwnProperty
to make sure you aren't deleting members of the object's prototype:
function deleteByValue(val) {
for(var f in fruits) {
if(fruits.hasOwnProperty(f) && fruits[f] == val) {
delete fruits[f];
}
}
}
var key = null;
for (var k in fruits){
if (fruits[k] === 'apple'){
key = k;
break;
}
}
if (key != null)
delete fruits[key];
Iterate over the object finding the corresponding key, then remove it (if found).
Don't know if this is efficient in terms of processing but using filter you can get this done in three lines:
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
var appleless_keys = Object.keys(fruits).filter(this_fruit => fruits[this_fruit] !== "apple");
appleless_obj = {};
appleless_keys.forEach(key => appleless_obj[key] = fruits[key]);
console.dir(appleless_obj);
Or as a function:
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
function remove_fruit(fruit_to_remove,fruits){
var new_keys = Object.keys(fruits).filter(this_fruit => fruits[this_fruit] !== fruit_to_remove);
new_obj = {};
new_keys.forEach(key => new_obj[key] = fruits[key]);
return new_obj;
}
console.dir(remove_fruit("apple",fruits));
Simplified using key deletion,
function stripProperty(o, v) {
return (delete o[Object.keys(o).splice(Object.values(o).indexOf(v), 1)])?o:0;
}
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
function stripProperty(o, v) {
return (delete o[Object.keys(o).splice(Object.values(o).indexOf(v), 1)])?o:0;
}
console.log(stripProperty(fruits, 'banana'));
Usage,
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
console.log(stripProperty(fruits, 'apple'))
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