Javascript 在关联数组上删除与拼接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8173210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete vs splice on associative array
提问by sarsnake
If I have a JS associative array which is from what I gather is really an object, and I wish to remove an element, using delete myArr[someId]
will set the element to undefined, whilst splice won't work at all... so what is the alternative for an associative array if I wish to delete an element (rather than setting it to undefined
)
如果我有一个来自我收集的 JS 关联数组实际上是一个对象,并且我希望删除一个元素,则使用delete myArr[someId]
会将元素设置为未定义,而 splice 根本不起作用......那么有什么替代方法对于关联数组,如果我想删除一个元素(而不是将其设置为undefined
)
回答by Geuis
The terminology in js can be confusing at first, so lets straighten that out.
js 中的术语起初可能会令人困惑,所以让我们理顺它。
Yes, pretty much everything in js is an object. However, there are differences in the data types.
是的,js 中的几乎所有东西都是一个对象。但是,数据类型存在差异。
An array can be used likeas associative array, but it's different than an object literal.
数组可以像关联数组一样使用,但它与对象字面量不同。
var x = []; //array
var y = {}; //object literal
An array is like a list. The keys of an array can be a numerical index or a string.
数组就像一个列表。数组的键可以是数字索引或字符串。
var x = ['a','b']; // x[0] === 'a', x[1] === 'b';
var x = [];
x['one'] = 'a';
x['blah'] = 'b';
Object literals are like dictionaries. They can be used in a similar way.
对象字面量就像字典。它们可以以类似的方式使用。
var x = { 0: 'a', 1: 'b' };
var x = { one: 'a', two: 'b' };
However, this is where you need to understand the differences.
但是,这是您需要了解差异的地方。
You can use an array likean object literal, but you can't use an object literal quite like an array.
您可以像使用对象字面量那样使用数组,但不能像使用数组那样使用对象字面量。
Arrays have the automated "length" property, that increments and decrements automatically based on the total number of elements in the array. You don't get this with object literals. Arrays also get all of the other array-specific methods like shift, unshift, splice, pop, push, etc. Object literals don't have those methods.
数组具有自动的“长度”属性,它根据数组中元素的总数自动递增和递减。你不会用对象字面量得到这个。数组还获得所有其他特定于数组的方法,如 shift、unshift、splice、pop、push 等。对象文字没有这些方法。
Let's talk about delete and what happens on an array and on an object literal.
让我们谈谈删除以及在数组和对象文字上发生的事情。
var x = ['a', 'b']; //["a", "b"]
delete x[0]; //[undefined, "b"]
var x = {0:'1', 1:'b'}// { 0:"1", 1:"b"}
delete x[0]; // { 1:"b" }
If you perform a delete on an element of an array, the length of the array doesn't change. The element index is preserved and the value is set to 'undefined';
如果对数组的元素执行删除,则数组的长度不会改变。元素索引被保留并且值被设置为'undefined';
Conversely, performing a delete on an object literal removes the key/value from the object.
相反,对对象文字执行删除操作会从对象中删除键/值。
Finally, if you want to remove an element from an array.
最后,如果你想从数组中删除一个元素。
var x = ['a', 'b'];
x.splice(0,1); //modifies x. ['b']
So, in summary use delete on object literals. Use splice on arrays.
因此,总而言之,对对象文字使用 delete 。在数组上使用拼接。
Hope this helps.
希望这可以帮助。
回答by Adam Rackis
There is no other option. myArr["someCrazyIndexYouHaventPreviouslyUsed"]
will return undefined; an associative array will always give you undefined
for indexes that don't exist.
没有其他选择。 myArr["someCrazyIndexYouHaventPreviouslyUsed"]
将返回未定义;关联数组将始终为您undefined
提供不存在的索引。
So delete myArr[someId]
will cause myArr
to treat someId
like every other index that doesn't exist—isn't that what you want?
所以 deletemyArr[someId]
会导致像myArr
对待someId
所有其他不存在的索引一样对待——这不是你想要的吗?