在关联数组中删除vs . splice。

时间:2023-02-06 21:27:09

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]会将该元素设置为undefined,而splice则不会工作…如果我想删除一个元素(而不是将它设置为undefined),那么关联数组还有什么可选的呢?

2 个解决方案

#1


155  

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 like as 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 like an 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.

让我们讨论一下delete,以及数组和对象文字上发生了什么。

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.

希望这个有帮助。

#2


5  

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”)将返回未定义;关联数组总是为不存在的索引提供未定义。

So delete myArr[someId] will cause myArr to treat someId like every other index that doesn't exist—isn't that what you want?

因此,删除myArr[someId]将导致myArr像其他所有不存在的索引一样对待someId——这难道不是您想要的吗?

#1


155  

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 like as 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 like an 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.

让我们讨论一下delete,以及数组和对象文字上发生了什么。

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.

希望这个有帮助。

#2


5  

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”)将返回未定义;关联数组总是为不存在的索引提供未定义。

So delete myArr[someId] will cause myArr to treat someId like every other index that doesn't exist—isn't that what you want?

因此,删除myArr[someId]将导致myArr像其他所有不存在的索引一样对待someId——这难道不是您想要的吗?