为什么删除keep Array元素? [英] Why does delete keep Array elements?

查看:68
本文介绍了为什么删除keep Array元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我在这里出现了关于堆栈溢出的问题-如何从javascript关联数组中删除对象?.令我感到震惊的是,被接受的答案既具有误导性又遭到了强烈反对,因此我强调了可能的陷阱.

Today I stumbled upon a question here on Stack Overflow - How do I remove objects from a javascript associative array?. What struck me was that the accepted answer was both misleading and heavily upvoted, so I highlighted the possible pitfall.

但是,在整理一个正确答案的同时,我意识到我不知道为什么对于delete保留元素 assign undefined有意义移除.

However, while cobbling together a corrective answer, I realized I have no idea as to why it makes sense for delete to keep elements assign undefined instead of removal.

var elements = new Array()
elements.push(NaN)
elements.push(NaN)
elements.push(NaN)
delete elements[1]
console.log("number of elements: ", elements.length)   // returns 3

背后是否有根据?

推荐答案

我意识到我不知道为什么delete分配未定义而不是删除是有意义的.

I realized I have no idea as to why it makes sense for delete to assign undefined instead of removal.

不是. delete从对象中删除属性,它将它们设置为undefined.这是一种简单的告诉方法:

It doesn't. delete removes properties from objects, it does not set them to undefined. Here's an easy way to tell:

var a = ['a', 'b', 'c'];
console.log(1 in a); // logs "true"
delete a[1];
console.log(1 in a); // logs "false"

请注意,在delete之后,a不再具有称为1的属性.完全没有.

Note that after the delete, a doesn't have a property called 1 anymore. At all.

对比:

var a = ['a', 'b', 'c'];
console.log(1 in a); // logs "true"
a[1] = undefined;
console.log(1 in a); // logs "true"

在那里,a仍然有一个名为1的属性,只是该属性的值是undefined.

There, a still has a property called 1, it's just that the property's value is undefined.

了解一下在JavaScript中数组并不是真正的数组很有用完全.它们只是对象,数组索引"只是属性名称(是字符串,是的,实际上,我们只是倾向于将它们写为数字),数组对属性名称都进行了特殊处理,这些属性名称都是数字(索引),一个特殊的length属性,以及它们从Array.prototype获得的一些功能.规范的第15.4节中对此进行了非常清晰的阐述.一旦确定了JavaScript数组并不是真正的数组,它们就会变得更加有意义. :-)

It's useful to understand that in JavaScript, arrays aren't really arrays at all. They're just objects, array "indexes" are just property names (which are strings — yes, really, we just tend to write them as numbers), arrays have special handling of property names that are all numeric (indexes), a special length property, and some functions they get from Array.prototype. This is very clearly laid out in Section 15.4 of the spec. Once you have it set firmly in your head that JavaScript arrays aren't really arrays, they make a lot more sense. :-)

从数组中删除数组的索引"属性不会更改其length(即使删除编号最大的属性也是如此);它只会在数组中造成一个洞(JavaScript数组"从本质上来说是稀疏数组;例如,它们之间可能有间隙).因此,在上面的第一个示例中,如果执行此操作,则会得到与获得的数组完全相同的数组:

Deleting an array "index" property from an array does not change its length (not even if you delete the highest-numbered one); it just creates a hole in the array (JavaScript "arrays" are sparse arrays by their nature; e.g., they can have gaps in them). So in my first example above, I get exactly the same array that I'd've gotten if I'd done this:

var a = [];
a[0] = 'a';
a[2] = 'c';

请注意间隙,数组没有1元素/属性.

Note the gap, the array has no 1 element/property.

如果您说:

var foo = a[3];

... foo可以由于两个完全不同的原因而获得值undefined:

...foo can get the value undefined for two completely different reasons:

  1. a具有一个名为3的属性,该属性的值是undefined,或者:
  2. a根本没有称为3的属性;对不具有该名称属性的对象的属性访问器操作的结果为undefined.规范以相当复杂的方式涵盖了此if-no-property-return- undefined,但大多以
  1. a has a property called 3 that has the value undefined, or:
  2. a has no property called 3 at all; the result of a property accessor operation on an object that doesn't have a property by that name is undefined. This if-no-property-return-undefined is covered by the spec in a fairly convoluted way, but mostly in Section 8.12.3.

这些是非常不同的东西.

These are very distinct things.

这篇关于为什么删除keep Array元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆