简体   繁体   English

JavaScript:创建未知维多维数组函数时的按引用传递问题

[英]JavaScript : Pass-by-reference issues when creating an unknown dimension multi dimensional array function

I'm posting this question because I am trying to make a function that allows someone to create a multi-dim array. 我发布此问题是因为我试图创建一个允许某人创建多维度数组的函数。 So, the user inputs an array of numbers which are the dimensions of the array (eg entering [2, 4, 3] would output a 2x4x3 multi-dim array) I have spent a couple of hours trying to imagine an algorithm that can do this in JS and I came up with this: 因此,用户输入了一个数字数组,该数字就是该数组的尺寸(例如,输入[2, 4, 3] 2,4,3 [2, 4, 3]将输出一个2x4x3多维数组)我花了几个小时试图想象一种算法可以做到在JS中,我想出了这个:

Note: I use Node.js v9.11.1 注意:我使用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)
}

My problem is that even if the array is well generated, some SA are passed by reference and not by value so when I do 我的问题是,即使数组生成良好,也会通过引用而不是通过值传递某些SA,所以当我这样做时

my_array = generate([2, 4, 3])
my_array[1][2][1] = "hi!" // Fill a random place with "hi!"

Then when I do console.log(my_array) , some other cases of the multi-dim array are filled with the same value. 然后,当我执行console.log(my_array) ,多维数组的其他一些情况将填充相同的值。 This means that somewhere, an array is passed by reference rather than passed by value which is strange because I checked the code multiple times and I don't find where this could come from (I use the Array.slice() method to "copy" the array) 这意味着在某个地方,数组是通过引用传递的,而不是通过值传递的,这很奇怪,因为我多次检查了代码,但我找不到它的来源(我使用Array.slice()方法来“复制“数组)

Have I missed something huge? 我错过了什么大事吗? Your help would be rather appreciated! 您的帮助将不胜感激!

To be honest, not sure how your trying to create your mult-dim array,.. 老实说,不确定您如何尝试创建多重数组。

But the first thing that springs to mind when seeing something like this, is recursion. 但是,当您看到类似这样的内容时,想到的第一件事就是递归。

eg.. 例如..

 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); 

I come up with this: 我想出了这个:

 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); 

Also using recursion to create multidimensional array. 还使用递归来创建多维数组。 But when creating arrays as You saw, have to be carefull about not passing references but real copies of arrays. 但是,如您所见,在创建数组时,必须小心不要传递引用,而要传递数组的真实副本。 Slice() will give You only shallow copy. Slice()只会给您浅拷贝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM