JavaScript : 创建未知维度多维数组函数时的按引用传递问题
JavaScript : Pass-by-reference issues when creating an unknown dimension multi dimensional array function
我发布这个问题是因为我正在尝试创建一个允许某人创建多维数组的函数。因此,用户输入一个数字数组,这些数字是数组的维度(例如输入 [2, 4, 3]
将输出一个 2x4x3 多维数组)
我花了几个小时试图想象一种可以在 JS 中执行此操作的算法,我想出了这个:
注:我用的是Node.jsv9.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的一些其他情况用相同的值填充。
这意味着在某个地方,数组是通过引用传递的,而不是通过值传递的,这很奇怪
因为我多次检查了代码,但找不到它的来源(我使用 Array.slice()
"copy" 数组的方法)
我错过了什么大事吗?
非常感谢您的帮助!
老实说,不确定您是如何尝试创建多维阵列的,..
但是当看到这样的东西时,首先想到的是递归。
例如..
function generate(dimensions) {
if (!dimensions.length) throw new Error("no dims?");
const dimsize = dimensions[0];
if (dimensions.length === 1) {
return new Array(dimsize).fill(null);
}
const ret = [];
const subdims = dimensions.slice(1);
for (let l = 0; l < dimsize; l+= 1)
ret.push(generate(subdims));
return ret;
}
const my_array = generate([2, 4, 3])
my_array[1][2][1] = "hi!"
console.log(my_array);
我想出了这个:
function generate(dims) {
if(dims.length > 0) {
let array = new Array(dims[0]).fill(0);
let childDims = dims.slice();
childDims.shift();
return array.map((el) => {
return generate(childDims);
});
} else return 0;
}
let foo = generate([2, 3, 2]);
foo[0][0][1] = 'hmmmm';
console.log(foo);
同样使用递归创建多维数组。但是当像你看到的那样创建数组时,必须小心不要传递引用,而是传递数组的真实副本。 Slice()
只会给你浅拷贝。
我发布这个问题是因为我正在尝试创建一个允许某人创建多维数组的函数。因此,用户输入一个数字数组,这些数字是数组的维度(例如输入 [2, 4, 3]
将输出一个 2x4x3 多维数组)
我花了几个小时试图想象一种可以在 JS 中执行此操作的算法,我想出了这个:
注:我用的是Node.jsv9.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的一些其他情况用相同的值填充。
这意味着在某个地方,数组是通过引用传递的,而不是通过值传递的,这很奇怪
因为我多次检查了代码,但找不到它的来源(我使用 Array.slice()
"copy" 数组的方法)
我错过了什么大事吗? 非常感谢您的帮助!
老实说,不确定您是如何尝试创建多维阵列的,..
但是当看到这样的东西时,首先想到的是递归。
例如..
function generate(dimensions) {
if (!dimensions.length) throw new Error("no dims?");
const dimsize = dimensions[0];
if (dimensions.length === 1) {
return new Array(dimsize).fill(null);
}
const ret = [];
const subdims = dimensions.slice(1);
for (let l = 0; l < dimsize; l+= 1)
ret.push(generate(subdims));
return ret;
}
const my_array = generate([2, 4, 3])
my_array[1][2][1] = "hi!"
console.log(my_array);
我想出了这个:
function generate(dims) {
if(dims.length > 0) {
let array = new Array(dims[0]).fill(0);
let childDims = dims.slice();
childDims.shift();
return array.map((el) => {
return generate(childDims);
});
} else return 0;
}
let foo = generate([2, 3, 2]);
foo[0][0][1] = 'hmmmm';
console.log(foo);
同样使用递归创建多维数组。但是当像你看到的那样创建数组时,必须小心不要传递引用,而是传递数组的真实副本。 Slice()
只会给你浅拷贝。