如何从javascript关联数组中删除对象?

本文探讨了如何从JavaScript的关联数组(对象)中删除元素,强调了对象和数组的区别,并提供了delete关键字、filter方法等多种删除方式。文章还提到了在不同场景下选择合适的数据结构和操作方法的重要性。
摘要由CSDN通过智能技术生成

本文翻译自:How do I remove objects from a javascript associative array?

Suppose I have this code: 假设我有这个代码:

var myArray = new Object();
myArray["firstname"] = "Bob";
myArray["lastname"] = "Smith";
myArray["age"] = 25;

Now if I wanted to remove "lastname"?....is there some equivalent of 现在,如果我想删除“姓氏”?....是否有一些相当于
myArray["lastname"].remove() ? myArray["lastname"].remove()

(I need the element gone because the number of elements is important and I want to keep things clean.) (我需要元素消失,因为元素的数量很重要,我想保持清洁。)


#1楼

参考:https://stackoom.com/question/1S0z/如何从javascript关联数组中删除对象


#2楼

While the accepted answer is correct, it is missing the explanation why it works. 虽然接受的答案是正确的,但它缺少解释原因。

First of all, your code should reflect the fact that this is NOT an array: 首先,您的代码应该反映出这不是一个数组的事实:

var myObject = new Object();
myObject["firstname"] = "Bob";
myObject["lastname"] = "Smith";
myObject["age"] = 25;

Note that all objects (including Array s) can be used this way. 请注意,所有对象(包括Array )都可以这种方式使用。 However, do not expect for standard JS array functions (pop, push,...) to work on objects! 但是,不要指望标准的JS数组函数(pop,push,...)能够处理对象!

As said in accepted answer, you can then use delete to remove the entries from objects: 如接受的答案中所述,您可以使用delete从对象中删除条目:

delete myObject["lastname"]

You should decide which route you wish to take - either use objects (associative arrays / dictionaries) or use arrays (maps). 您应该决定要采用哪条路径 - 使用对象(关联数组/字典)或使用数组(地图)。 Never mix the two of them. 永远不要混淆他们两个。


#3楼

As other answers have noted, what you are using is not a Javascript array, but a Javascript object, which works almost like an associative array in other languages except that all keys are converted to strings. 正如其他答案所指出的那样,你所使用的不是Javascript数组,而是一个Javascript对象,它几乎就像其他语言中的关联数组一样,除了所有键都转换为字符串。 The new Map stores keys as their original type. Map将密钥存储为其原始类型。

If you had an array and not an object, you could use the array's .filter function, to return a new array without the item you want removed: 如果你有一个数组而不是一个对象,你可以使用数组的.filter函数来返回一个没有你想删除的项目的新数组:

var myArray = ['Bob', 'Smith', 25];
myArray = myArray.filter(function(item) {
    return item !== 'Smith';
});

If you have an older browser and jQuery, jQuery has a $.grep method that works similarly: 如果你有一个较旧的浏览器和jQuery,jQuery有一个类似的$.grep方法

myArray = $.grep(myArray, function(item) {
    return item !== 'Smith';
});

#4楼

If for whatever reason the delete key is not working (like it wasnt working for me ) 如果由于某种原因删除键不起作用(就像它不适合我)

You can splice it out and then filter the undefined values 您可以将其拼接出来,然后过滤未定义的值

// to cut out one element via arr.splice(indexToRemove, numberToRemove);
array.splice(key, 1)
array.filter(function(n){return n});

Dont try and chain them since splice returns removed elements; 不要尝试链接它们,因为splice返回删除的元素;


#5楼

如果您的项目中有underscore.js依赖项,它非常直接 -

_.omit(myArray, "lastname")

#6楼

By using the "delete" keyword, it will delete the array element from array in javascript. 通过使用"delete"关键字,它将从javascript中的数组中删除数组元素。

For example, 例如,

Consider following statements. 考虑以下陈述。

var arrayElementToDelete = new Object();

arrayElementToDelete["id"]           = "XERTYB00G1"; 
arrayElementToDelete["first_name"]   = "Employee_one";
arrayElementToDelete["status"]       = "Active"; 

delete arrayElementToDelete["status"];

Last line of the code will remove the array element who's key is "status" from the array. 代码的最后一行将从数组中删除密钥为“status”的数组元素。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值