Home > OS >  Cannot push a 2d array into an array
Cannot push a 2d array into an array

Time:05-26

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:

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. 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.

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

CodePudding user response:

You need to nest the two for loops:

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 }

CodePudding user response:

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

You can see that in the log in the snippet below: /**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.

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)

  • Related