javascript 由 Array 构造函数创建的 undefined 数组上的 forEach
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23460301/
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
forEach on array of undefined created by Array constructor
提问by Tukkan
I am just wondering why it is not possible to make forEach on array of undefined.
我只是想知道为什么不可能在未定义的数组上创建 forEach。
Code:
代码:
var arr = new Array(5); // [undefined x 5]
//ES5 forEach
arr.forEach(function(elem, index, array) {
console.log(index);
});
//underscore each
_.each(arr, function(elem, index, array) {
console.log(index);
});
Both examples do not execute function.
两个例子都没有执行函数。
Now to make foreach, I have to make:
现在要制作 foreach,我必须制作:
var arr = [0,0,0,0,0];
Then make forEach on it.
然后让 forEach 就可以了。
I am trying to make an array with specified size and loop through it, avoiding for
loop. I think that it is clearer using forEach than for loop.
With array with length 5 it is not a problem, but it would be ugly with bigger arrays.
我正在尝试创建一个具有指定大小的数组并循环遍历它,避免for
循环。我认为使用 forEach 比使用 for 循环更清晰。对于长度为 5 的数组,这不是问题,但是对于更大的数组,它会很丑陋。
Why there is a problem looping through array of undefined values ?
为什么在遍历未定义值数组时会出现问题?
回答by soulcheck
Array(5)
is essentialy equivalent to
Array(5)
本质上等同于
var arr = [];
arr.length = 5;
In javascript changing array's length doesn't set any values for it's numeric properties nor does it define those properties in the array object. So numeric properties are undefined instead of having undefined value. You can check it by using:
在 javascript 中,更改数组的长度不会为其数字属性设置任何值,也不会在数组对象中定义这些属性。所以数字属性是未定义的,而不是具有未定义的值。您可以使用以下方法检查它:
Object.keys(arr)
When iterating javascript iterates through numeric properties of the array, so if these don't exist, there is nothing to iterate over.
当迭代 javascript 时会迭代数组的数字属性,所以如果这些属性不存在,就没有什么可以迭代的。
You can check it by doing:
您可以通过执行以下操作来检查它:
var arr = Array(5)
//prints nothing
arr.forEach(function(elem, index, array) {
console.log(index);
});
arr[3] = 'hey'
//prints only 'hey' even though arr.length === 5
arr.forEach(function(elem, index, array) {
console.log(index);
});
The following code:
以下代码:
var arr = [undefined, undefined];
creates and array of length ===2
and sets the both numeric properties 0 and 1 to undefined.
创建和数组,length ===2
并将数值属性 0 和 1 设置为 undefined。
回答by cookie monster
Looking at a simplified implementation of .forEach()
may help.
查看 的简化实现.forEach()
可能会有所帮助。
Array.prototype.my_for_each = function(callback, thisArg) {
for (var i = 0; i < this.length; i++) {
if (i in this) {
callback.call(thisArg, this[i], i, this);
}
}
};
So you can see that what happens is that the method doesiterate the entire Array (according to the spec), but it only invokes the callback if the member actually exists. It checks by looking for the property (the index)using the in
operator, which tests to see if the object either has or inherits the property.
所以你可以看到发生的事情是该方法确实迭代了整个 Array (根据规范),但它只在成员实际存在时调用回调。它通过使用运算符查找属性(索引)来进行in
检查,该运算符测试对象是否具有或继承了该属性。
If in
shows that the index exists, the callback is invoked.
如果in
显示索引存在,则调用回调。
So given this Array:
所以给定这个数组:
var arr = ["foo", "bar", "baz"];
This will output all 3 items:
这将输出所有 3 个项目:
arr.my_for_each(function(item) {
console.log(item);
});
// "foo" "bar" "baz"
But if we use delete
to remove a member, it leaves a hole in the Array, which will now get passed over.
但是如果我们使用delete
删除成员,它会在 Array 中留下一个洞,现在将被忽略。
delete arr[1];
arr.my_for_each(function(item) {
console.log(item);
});
// "foo" "baz"
When you created an Array using Array(5)
, it created one without any members, but with the .length
set to 5
. So this is an example of a sparse Array (very sparse in this instance). Because none of the indices will be found by in
, the callback is never invoked.
当您使用 创建一个数组时Array(5)
,它创建了一个没有任何成员的数组,但.length
设置为5
。所以这是一个稀疏数组的例子(在这个例子中非常稀疏)。由于 找不到任何索引in
,因此永远不会调用回调。