【问题标题】:JavaScript : Pass-by-reference issues when creating an unknown dimension multi dimensional array functionJavaScript:创建未知维度多维数组函数时的引用传递问题
【发布时间】: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


【解决方案1】:

说实话,不知道您是如何尝试创建多维数组的,..

但是当看到这样的东西时,首先想到的是递归。

例如..

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

【讨论】:

    【解决方案2】:

    我想出了这个:

    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() 只会给你浅拷贝。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2013-04-17
      • 1970-01-01
      • 2010-10-06
      • 2010-11-18
      • 1970-01-01
      • 2020-08-27
      相关资源
      最近更新 更多