如何從javascript關聯數組中刪除對象?

本文翻譯自: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”的數組元素。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章