Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete a javascript object item by value?

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?

like image 774
amlane86 Avatar asked Mar 28 '12 15:03

amlane86


People also ask

How do I remove an item from an array by value?

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.

How can I delete an object in JavaScript?

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.

How do you clear the value of a object?

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!

How do I remove a property from an array of objects?

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.


4 Answers

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];
        }
    }
}
like image 93
mdm Avatar answered Oct 02 '22 20:10

mdm


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).

like image 40
Brad Christie Avatar answered Oct 02 '22 21:10

Brad Christie


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));
like image 30
it Haffens Avatar answered Oct 02 '22 21:10

it Haffens


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'))
like image 23
Arun Panneerselvam Avatar answered Oct 02 '22 21:10

Arun Panneerselvam