【发布时间】:2018-06-25 13:05:32
【问题描述】:
我发布这个问题是因为我正在尝试创建一个允许某人创建多维数组的函数。因此,用户输入一个数字数组,它们是数组的维度(例如,输入 [2, 4, 3] 将输出一个 2x4x3 多维数组)
我花了几个小时试图想象一种可以在 JS 中做到这一点的算法,我想出了这个:
注意:我使用 Node.js v9.11.1
function generate(dimensions) {
// SA = sub-array (I will use this several times here)
// This array will store every SAs of the multi-dim array
// E.g for a 2x4x3 array, it will store a 2-item array, a 4-item array and a 3-item array
var arrays = []
// This fills `arrays` with the SAs
for (var i = 0; i < dimensions.length; i++) arrays.push(new Array(dimensions[i]).slice(0))
// Here it gets a bit complex (at least for me!)
// So what we do is that for each SA (except last), we fill it with copies of the current+1 SA
// So the SA at index 1 will be filled with copies of the array at index 2
// And the array at index 0 will be filled with arrays of index 1 (which was already filled because our for loop starts from the end)
// The array at index 0 is our final multi-dim array
// Goes from the before last SA to the first
for (var current = dimensions.length-2; current !== -1; current--) {
// Fills the current SA with index+1 SA
for (var i = 0; i < arrays[current].length; i++) arrays[current][i] = arrays[current+1].slice(0)
}
// Returns first array, the complete one
return arrays[0].slice(0)
}
我的问题是,即使数组生成良好,一些 SA 是通过引用而不是通过值传递的,所以当我这样做时
my_array = generate([2, 4, 3])
my_array[1][2][1] = "hi!" // Fill a random place with "hi!"
然后当我做console.log(my_array)时,multi-dim数组的其他一些情况被填充了相同的值。
这意味着在某处,数组是通过引用传递而不是通过值传递,这很奇怪
因为我多次检查了代码,但找不到它的来源(我使用Array.slice()
“复制”数组的方法)
我错过了什么大事吗? 非常感谢您的帮助!
【问题讨论】:
-
我认为您的问题是使用
slice(),它为您提供了数组的副本,而不是其元素的副本。您需要使用可以为您提供深层副本的东西。
标签: javascript arrays multidimensional-array pass-by-reference dynamically-generated