简体   繁体   English

无法将二维数组推入数组

[英]Cannot push a 2d array into an array

Sorry if the title is unclear/badly worded, I'm a fairly new coder who sucks at remembering correct terminology.抱歉,如果标题不清楚/措辞不当,我是一个相当新的编码员,不擅长记住正确的术语。 I have been trying to create a 3d array in Javascript that I want to look like this:我一直在尝试在 Javascript 中创建一个 3d 数组,我希望它看起来像这样:

var errorCountList = [
  [
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]
  ],
  [
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]
  ],
  [
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0]
  ]
];

However, I want the number of [0, 0, 0, 0, 0, 0] lists in the 2d/3d parts of the list to be able to vary in length, if that makes any sense.但是,如果有任何意义,我希望列表的 2d/3d 部分中的 [0, 0, 0, 0, 0, 0] 列表的长度能够变化。 So, as I result, I have been using a series of loops to push lists into a 2d array before pushing these 2d arrays into another array to make a 3d array.因此,作为我的结果,我一直在使用一系列循环将列表推入 2d 数组,然后再将这些 2d 数组推入另一个数组以制作 3d 数组。

for (count3 = 0; count3 < test.length; count3++)    {
        part2.push([0, 0, 0, 0, 0, 0]);
    }
for (count5 = 0; count5 < restrictionValues.length; count5++)   {
        errorCountList.push(part2);
    }

with test and restrictionValues being changing list lengths.测试和限制值正在改变列表长度。 However, while when I print out the list using print(JSON.stringify(errorCountList));但是,当我使用print(JSON.stringify(errorCountList));打印出列表时it visually appears the same as the errorCountList I set up, the values it displays after I run the rest of my code are radically different.它在视觉上看起来与我设置的 errorCountList 相同,在我运行其余代码后它显示的值完全不同。 How can I fix this issue?我该如何解决这个问题? Thanks for the help in advance, this is my first time posting here so sorry about any issues in wording/formatting.提前感谢您的帮助,这是我第一次在这里发帖,对措辞/格式方面的任何问题感到抱歉。

You need to nest the two for loops:您需要嵌套两个for循环:

 test = [1, 2]; restrictionValues = [1, 2, 3]; errorCountList = []; for (count5 = 0; count5 < restrictionValues.length; count5++) { part2 = []; for (count3 = 0; count3 < test.length; count3++) { part2.push([0, 0, 0, 0, 0, 0]); } errorCountList.push([...part2]); } console.log(JSON.stringify(errorCountList))
 .as-console-wrapper { max-height: 100% !important; top: 0 }

You're probably pushing references of one array, rather than pushing nine different arrays.您可能正在推送一个数组的引用,而不是推送九个不同的数组。 In order to save memory, javascript never creates a new array when reassigning an array (and the same goes with or objects).为了节省内存,javascript 在重新分配数组时从不创建新数组(or 对象也是如此)。

You can see that in the log in the snippet below: /**ref:2**/,您可以在下面片段的日志中看到: /**ref:2**/,

 let part2 = [], errorCountList = [], restrictionValues = Array(3), test = Array(3); for (let count3 = 0; count3 < test.length; count3++) { part2.push([0, 0, 0, 0, 0, 0]); } for (let count5 = 0; count5 < restrictionValues.length; count5++) { errorCountList.push(part2); } console.log(errorCountList)

I would instead use a combination of Array(n) , to create an empty array, and Array.fill(n) to fill that empty array with zeroes.我会改为使用Array(n)的组合来创建一个空数组,并Array.fill(n)用零填充该空数组。

 let errorCountList = [], restrictionValues = 3, maxLength = 6; const FILLER = 0; const random = (max, min = 1) => { return Math.floor(Math.random() * max + min); }; for (let count5 = 0; count5 < restrictionValues; count5++) { errorCountList.push(Array( random(maxLength) ).fill(FILLER)); } console.log(errorCountList)

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

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