本文介绍了为什么 Array.apply(null, [args]) 在处理稀疏数组时表现不一致?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了以下 SO 上的代码片段 以帮助使用默认值快速填充数组:>

Array.apply(null, new Array(3)).map(function() {return 0;});

考虑到 Array 构造函数和 apply 方法的行为,上面的代码段也可以改写为:

Array.apply(null, [undefined, undefined, undefined]).map(function() {return 0;});

在处理您希望使用默认值填充的稀疏数组时,此技术也很有用:

var sparseArr = [3,,,4,1,,],DenseArr = Array.apply(null, sparseArr).map(function(e) {返回 e === 未定义?0 : e;});//denseArr = [3,0,0,4,1,0]

然而,其中出现了两个奇怪的现象:

  1. 如果 sparseArr 的最后一项未定义,则该项不会映射到 denseArr
  2. 如果 sparseArr 仅包含一个词条(例如 sparseArr = [1])或一个词条后跟单个未定义词条(例如 sparseArr =[1,]),结果 denseArr 等于 [undefined x 1]

谁能解释这种行为?

解决方案

不 - 正如您刚刚看到的,数组构造函数创建稀疏数组,因此应将其重写为 [,,,].

如果 sparseArr 的最后一项未定义

没有.你忘记了尾随逗号,从 EcmaScript 5 开始这是可选的.实际上 [1] 只是等价于 [1,] (两者的长度都是 1).

要获得稀疏的插槽",您必须添加额外的逗号:

[]//空数组[,]//空数组[,,]//[未定义 x 1][,,,]//[未定义 x 2]

如果 sparseArr 只包含一个词条,结果 denseArr 等于 [undefined x N]

考虑调用 apply方法:

Array.apply(null, [3,,4,1]) ≡ Array(3, undefined, 4, 1)Array.apply(null, [3,4]) ≡ Array(3, 4)Array.apply(null, [1]) ≡ Array(1)

并且您知道 Array 构造函数 在使用单个数字参数调用时会这样做 - 它会创建一个该长度的稀疏数组......

I recently discovered the following snippet of code on SO to aid in quickly populating an array with default values:

Array.apply(null, new Array(3)).map(function() {return 0;});

Given the behavior of the Array constructor and the apply method, the above snippet can also be rewritten as such:

Array.apply(null, [undefined, undefined, undefined]).map(function() {return 0;});

This technique is also useful when dealing with sparse arrays that you wish to populate with default values:

var sparseArr = [3,,,4,1,,],
    denseArr = Array.apply(null, sparseArr).map(function(e) {
      return e === undefined ? 0 : e;
    });

// denseArr = [3,0,0,4,1,0]

However it is therein that two oddities arise:

  1. If the final term of of sparseArr is undefined, that term is not mapped in denseArr
  2. If sparseArr contains only a single term (e.g. sparseArr = [1]) or a single term followed by a single trailing undefined term (e.g. sparseArr = [1,]), the resulting denseArr equals [undefined x 1]

Can anyone explain this behavior?

解决方案

No - as you just have seen, the array constructor creates sparse arrays so it should be rewritten as [,,,].

Nope. You're forgetting about trailing commata, which are optional since EcmaScript 5. Actually [1] is just equivalent to [1,] (both have a length of 1).

To get sparse "slots", you will have to add additional commata:

[] // empty array
[,] // empty array
[,,] // [undefined x 1]
[,,,] // [undefined x 2]

Consider what it means to call the apply method:

Array.apply(null, [3,,4,1]) ≡ Array(3, undefined, 4, 1)
Array.apply(null, [3,4]) ≡ Array(3, 4)
Array.apply(null, [1]) ≡ Array(1)

And you know what the Array constructor does when being called with a single numeric arguments - it creates a sparse array of that length…

这篇关于为什么 Array.apply(null, [args]) 在处理稀疏数组时表现不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 23:06