How to remove javascript object object by value?

Take, for example, the following object:

var fruits = { "red" : "apple", "blue" : "blueberry", "yellow" : "banana" } 

I know that I can use delete fruits["red"] to delete it by key name, but how can I delete an object by fruit name?

+4
source share
8 answers

Have you tried something like this?

 function deleteByValue(val) { for(var f in fruits) { if(fruits[f] == val) { delete fruits[f]; } } } 

And according to Rocket's comment, you can check hasOwnProperty to make sure that you are not deleting participants from the object prototype:

 function deleteByValue(val) { for(var f in fruits) { if(fruits.hasOwnProperty(f) && fruits[f] == val) { delete fruits[f]; } } } 
+7
source
 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 delete it (if found).

+1
source

You can use the splicing method

 fruits.splice($.inArray("red", fruits), 1); 

But this, of course, uses jQuery.

You can also use this extension:

 Array.prototype.remove = function () { var what, a = arguments, L = a.length, ax; while (L && this.length) { what = a[--L]; while ((ax = this.indexOf(what)) != -1) { this.splice(ax, 1); } } return this; } 
0
source

how about this?

 function delteByValue(a){ fruits.foreach( function( k, v ) { if (fruits[v] == a){ delete fruits[k]; } }); } 
0
source

I think it's nice to create a function and override Object.prototype:

 /** * @autor Javier Cobos * @param value The value to look for * @return true if founded deleted, false if not */ Object.prototype.removeByValue = function(value){ var i; for(i in this){ if(this.hasOwnProperty(i)) if(value === this[i]){ delete(this[i]); return true; } } return false; } // Example var fruits = { "red" : "apple", "blue" : "blueberry", "yellow" : "banana" } fruits .removeByValue("apple"); 

So we have a new method for each individual object in our script :)

0
source

I know that you have some acceptable answers at the moment ... I'm going to include a generic function in it ...

 // NOTE, replace "Object.removePropertyByValue" // with say "jQuery.removePropertyByValue" or "_.removePropertyByValue" // to create a jQuery or Underscore extension. Object.removePropertyByValue = function(obj, val) { //store list of properties to remove var propsToRemove = []; for (var prop in obj) { if (obj.hasOwnProperty(prop) && obj[prop] === val) { //save the property name here, deleting while enumerating is a bad idea. propsToRemove.push(prop); } } //remove all the items stored. while (propsToRemove.length) delete obj[propsToRemove.pop()]; } 

From here you can call: Object.removePropertyByValue(fruits, "red");

0
source

use this function

 function delobj(x,n,v){ n = "elem."+n; var u = x.filter(function(elem, index, self){ if(eval(n) != v){ return index == self.indexOf(elem); } }) return u; } 

use

 delobj(object "object name",string "name of element",string "value of element") 
0
source
 var fruits = { "red" : "apple", "blue" : "blueberry", "yellow" : "banana" } delete fruits.red; // or use => delete fruits['red']; console.log(fruits); 

this removes fruit.red

-2
source

Source: https://habr.com/ru/post/1403929/


All Articles