例如,以下对象:
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
我知道我可以使用delete fruits["red"]
按键名删除它,但是如何通过水果名称删除对象项呢?
答案 0 :(得分:8)
你尝试过这样的事吗?
function deleteByValue(val) {
for(var f in fruits) {
if(fruits[f] == val) {
delete fruits[f];
}
}
}
根据Rocket的评论,您可能需要检查hasOwnProperty
以确保不删除对象原型的成员:
function deleteByValue(val) {
for(var f in fruits) {
if(fruits.hasOwnProperty(f) && fruits[f] == val) {
delete fruits[f];
}
}
}
答案 1 :(得分:1)
var key = null;
for (var k in fruits){
if (fruits[k] === 'apple'){
key = k;
break;
}
}
if (key != null)
delete fruits[key];
迭代找到相应键的对象,然后将其删除(如果找到)。
答案 2 :(得分:0)
function delteByValue(a){
fruits.foreach( function( k, v ) {
if (fruits[v] == a){
delete fruits[k];
}
});
}
答案 3 :(得分:0)
我认为创建一个函数并覆盖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");
这样我们脚本中的每个Object都有一个新方法:)
答案 4 :(得分:0)
我知道你现在有几个可以接受的答案......我将为此包含一个通用函数......
// 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()];
}
从此处您应该可以致电:Object.removePropertyByValue(fruits, "red");
答案 5 :(得分:0)
不知道这在处理方面是否有效,但使用过滤器可以在三行中完成:
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);
或作为一项功能:
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));
答案 6 :(得分:0)
使用密钥删除进行简化,
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'));
用法,
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
console.log(stripProperty(fruits, 'apple'))
答案 7 :(得分:-1)
您可以使用拼接方法
fruits.splice($.inArray("red", fruits), 1);
但是这当然使用jQuery。
您也可以使用此扩展程序:
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;
}
答案 8 :(得分:-1)
使用此功能
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;
}
像这样使用
delobj(object "object name",string "name of element",string "value of element")
答案 9 :(得分:-3)
var fruits = {
"red" : "apple",
"blue" : "blueberry",
"yellow" : "banana"
}
delete fruits.red; // or use => delete fruits['red'];
console.log(fruits);
这会删除fruits.red