JavaScript的forEach循环如何决定跳过或遍历数组中的“undefined”和“null”元素?

时间:2022-01-20 01:15:48

I know that forEach method will iterate through an array object and will skip all array elements which are either null or undefined. I've an example below:

我知道forEach方法将迭代一个数组对象,并将跳过所有null或undefined的数组元素。我有一个例子如下:

var a = [1,2,3,,5,6];
var b = [1,2,3,undefined,5,6];

var fn = function(arr){
    arr.forEach(function(currentValue, index, array){
      console.log(currentValue);
    });
};  

fn(a); //Prints on console (separated by newline): 1 2 3 5 6
fn(b); //Prints on console (separated by newline): 1 2 3 undefined 5 6

In above example,

在上面的例子中,

  • when fn(a) is executed, the forEach loop ignores the 4th element a[3] which is undefined.
  • 当执行fn(a)时,forEach循环忽略未定义的第4个元素a [3]。

  • But, when fn(b) is executed, the forEach loop iterates through the 4th element b[3] which is also undefined.
  • 但是,当执行fn(b)时,forEach循环遍历第4个元素b [3],这也是未定义的。

What is the difference between a[3] and b[3] here? Why forEach loop didn't skip b[3]?

a [3]和b [3]有什么区别?为什么forEach循环没有跳过b [3]?

2 个解决方案

#1


1  

According to the specifications of ECMAScript 5.1, missing array items are elided.

根据ECMAScript 5.1的规范,省略了丢失的数组项。

Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined.

每当元素列表中的逗号前面没有AssignmentExpression(即开头的逗号或另一个逗号后面的逗号)时,缺少的数组元素会增加数组的长度并增加后续元素的索引。未定义Elided数组元素。

Since they are undefined, behaviour is unpredictable and there is no definition as to whether these should be treated as normal array items like undefined. I suspect that in some browsers, undefined may be returned, but this doesn't appear to be the case in Chrome. Here's what your first example of [1, 2, 3,, 5, 6] evaluates to:

由于它们是未定义的,因此行为是不可预测的,并且没有关于这些是否应被视为未定义的正常数组项的定义。我怀疑在某些浏览器中,可能会返回undefined,但在Chrome中似乎并非如此。以下是[1,2,3,5,6]的第一个示例评估为:

[1, 2, 3, undefined × 1, 5, 6]

However, [1, 2, 3, undefined, 5, 6] just evaluates to:

但是,[1,2,3,undefined,5,6]只评估为:

[1, 2, 3, undefined, 5, 6]

As we can see, these are subtly different, so the behaviour is not the same even though they look superficially similar.

正如我们所看到的,这些是微妙的不同,所以即使它们看起来表面上相似,行为也不尽相同。

#2


0  

If you print the arrays in the console you will get:

如果您在控制台中打印阵列,您将获得:

var a = [1,2,3,,5,6];
var b = [1,2,3,undefined,5,6];
console.log(a);
console.log(b);

Console

    >[1, 2, 3, 4: 5, 5: 6]
    >
    0:1
    1:2
    2:3
    4:5
    5:6
    length:6


    >[1, 2, 3,undefined, 5,6]
    >
    0:1
    1:2
    2:3
    3:undefined
    4:5
    5:6
    length:6

So there are not element in index 3.

所以索引3中没有元素。

#1


1  

According to the specifications of ECMAScript 5.1, missing array items are elided.

根据ECMAScript 5.1的规范,省略了丢失的数组项。

Whenever a comma in the element list is not preceded by an AssignmentExpression (i.e., a comma at the beginning or after another comma), the missing array element contributes to the length of the Array and increases the index of subsequent elements. Elided array elements are not defined.

每当元素列表中的逗号前面没有AssignmentExpression(即开头的逗号或另一个逗号后面的逗号)时,缺少的数组元素会增加数组的长度并增加后续元素的索引。未定义Elided数组元素。

Since they are undefined, behaviour is unpredictable and there is no definition as to whether these should be treated as normal array items like undefined. I suspect that in some browsers, undefined may be returned, but this doesn't appear to be the case in Chrome. Here's what your first example of [1, 2, 3,, 5, 6] evaluates to:

由于它们是未定义的,因此行为是不可预测的,并且没有关于这些是否应被视为未定义的正常数组项的定义。我怀疑在某些浏览器中,可能会返回undefined,但在Chrome中似乎并非如此。以下是[1,2,3,5,6]的第一个示例评估为:

[1, 2, 3, undefined × 1, 5, 6]

However, [1, 2, 3, undefined, 5, 6] just evaluates to:

但是,[1,2,3,undefined,5,6]只评估为:

[1, 2, 3, undefined, 5, 6]

As we can see, these are subtly different, so the behaviour is not the same even though they look superficially similar.

正如我们所看到的,这些是微妙的不同,所以即使它们看起来表面上相似,行为也不尽相同。

#2


0  

If you print the arrays in the console you will get:

如果您在控制台中打印阵列,您将获得:

var a = [1,2,3,,5,6];
var b = [1,2,3,undefined,5,6];
console.log(a);
console.log(b);

Console

    >[1, 2, 3, 4: 5, 5: 6]
    >
    0:1
    1:2
    2:3
    4:5
    5:6
    length:6


    >[1, 2, 3,undefined, 5,6]
    >
    0:1
    1:2
    2:3
    3:undefined
    4:5
    5:6
    length:6

So there are not element in index 3.

所以索引3中没有元素。