为什么Array.apply(NULL,[参数])与稀疏矩阵打交道时采取行动不一致? [英] Why does Array.apply(null, [args]) act inconsistently when dealing with sparse arrays?
问题描述
最近我发现下面的上SO code的片段以帮助快速填充使用默认值的数组
Array.apply(空,新的Array(3))图(函数(){返回0;});
由于Array构造函数的行为和应用方法,上面的代码也可以写成这样:
Array.apply(NULL,[不确定的,不确定的,不确定])地图(函数(){返回0;});
这与技术,您希望使用默认值来填充稀疏数组打交道时也很有用:
VAR sparseArr = [3 ,,, 4,1 ,,]
denseArr = Array.apply(NULL,sparseArr).MAP(函数(五){
返回e的===未定义? 0:电子;
});// denseArr = [3,0,0,4,1,0]
然而,它是其中两个出现怪事:
- 如果的最后期限
sparseArr
是不确定的,这个词是不是在denseArr
映射 - 如果
sparseArr
只包含一个词(如sparseArr = [1]
)或单个词后面一个尾随不确定的期限(如sparseArr = [1]
),所产生的denseArr
等于[未定义×1]
任何人都可以解释这种现象?
新阵列(3)
[...]也可以写成[不确定的,不确定的,不确定]
块引用>没有 - 就像你刚才所看到的,数组构造函数创建稀疏数组所以应该写成
。[,,,]
如果的sparseArr最后期限是不确定的。
块引用>不。你忘记了尾随commata,这是可选的,因为ECMAScript的5其实
[1]
只是等同于[1]
(两者都有的长度1
)。要获得疏槽中,你将不得不增加额外的commata:
[] //空数组
[,] //空数组
[,,] // [未定义×1]
[,,,] // [未定义×2]
如果
sparseArr
只包含一个词,由此产生的denseArr
等于[未定义点¯x N]
块引用>考虑这意味着什么叫<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"><$c$c>apply$c$c>方法:
Array.apply(NULL,[3,4,1])≡阵列(3,不确定,4,1)
Array.apply(空,[3,4])≡阵列(3,4)
Array.apply(空,[1])≡阵列(1)和你知道的<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array\"><$c$c>Array$c$c>构造并当被称为一个单一的数字参数 - 它会创建一个长度的稀疏数组...
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:
- If the final term of of
sparseArr
is undefined, that term is not mapped indenseArr
- 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 resultingdenseArr
equals[undefined x 1]
Can anyone explain this behavior?
解决方案
new Array(3)
[…] can also be rewritten as[undefined, undefined, undefined]
No - as you just have seen, the array constructor creates sparse arrays so it should be rewritten as
[,,,]
.If the final term of of sparseArr is undefined
Nope. You're forgetting about trailing commata, which are optional since EcmaScript 5. Actually
[1]
is just equivalent to[1,]
(both have a length of1
).To get sparse "slots", you will have to add additional commata:
[] // empty array [,] // empty array [,,] // [undefined x 1] [,,,] // [undefined x 2]
If
sparseArr
contains only a single term, the resultingdenseArr
equals[undefined x N]
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,[参数])与稀疏矩阵打交道时采取行动不一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!