Array(n)和[... Array(n)]之间的区别

时间:2018-08-22 08:56:42

标签: javascript arrays map-function spread-syntax

为什么以下代码中的map()输出有所不同?

var y = [1,2,2,1];

var t = y.map(ind => [...Array(ind)].map((_,i) => ind+""+i));
// This makes [ [ '10' ], [ '20', '21' ], [ '20', '21' ], [ '10' ] ]

var t1 = y.map(ind => Array(ind).map((_,i) => ind+""+i));
//[ [ <1 empty item> ], [ <2 empty items> ], [ <2 empty items> ], [ <1 empty item> ] ]

0 个答案:

没有答案