Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Writing to multidimensional arrays not working correctly?

The simplified codeblock below clearly shows the dilemma I am facing.

When writing to the multidimensional array below in the TestArrays function the loop should write a single item to each location, but when you print the layers to the console every item exists in every layer. What am I missing? This seems like a simple problem and one I have solved in other languages but this is leaving me stumped.

The layers where nothing was written has nothing in it as it should but if anything was written then everything was. I have simulated this by not writing to the second address.

function ArrayND(initVal) {
  /***********************************************************************************************
   * This function found at Stack Overflow at the link below
   * https://stackoverflow.com/a/33362121
   * This function will be used to create the initial structure of the arrays
   ***********************************************************************************************/
  var args = arguments;
  var dims = arguments.length - 1

  function ArrayCreate(cArr, dim) {
    if (dim < dims) {
      for (var i = 0; i < args[1 + dim]; i++) {
        if (dim == dims - 1) cArr[i] = initVal
        else cArr[i] = ArrayCreate([], dim + 1)
      }
      return cArr
    }
  }
  return ArrayCreate([], 0)
}

function TestArray() {
  let myArray = ArrayND("blank", 3, 8, 4, 1);
  let emptyArray = [];
  let innerArray;
  let count = 1;

  for (let outer = 1; outer <= 2; outer++) {
    for (let middle = 1; middle <= 7; middle++) {
      for (let inner = 1; inner <= 3; inner++) {
        if (count != 2) {
          myArray[outer][middle][inner].push(emptyArray);
          let innerArray = [count, "Data1", "Data2"];
          myArray[outer][middle][inner][1].push(innerArray);
        }
        count++;
      }
    }
  }
  for (let outer = 1; outer <= 2; outer++) {
    for (let middle = 1; middle <= 7; middle++) {
      for (let inner = 1; inner <= 3; inner++) {
        console.log("Data at location: ".outer, middle, inner, "conatains: ", myArray[outer][middle][inner]);
      }
    }
  }
}

TestArray();

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

@Barmar answered the question above in his comment.

This had to do with Javascript seeing the emptyArray as a single instance regardless of where it was instantiated in the inner loop.

the code was changed to .push([]) and that fixed the issue. Just required a comment so other coders know why an empty array was added instead of in the declarations area at the top of the function.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!