0

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();
Barmar
  • 741,623
  • 53
  • 500
  • 612
Olsonm76
  • 45
  • 1
  • 4
  • You're pushing the same `emptyArray()` every time through the loop. So when you modify it, you're modifying it in all the places. Move `let emptyArray = []` inside the inner loop, or just change `push(emptyArray)` to `push([])` – Barmar Mar 30 '22 at 08:15
  • That fixed it. But why? I set it with empty array on the outside to make it readable and expected a new instance of it each time it was written. Thank you immensely. – Olsonm76 Mar 30 '22 at 15:10
  • Why would you expect a new instance? Every time you use the variable `emptyArray` you're referring to the same instance, it doesn't make a copy. How else could you pass an array to a function that modifies the array in place? – Barmar Mar 30 '22 at 15:14
  • because at the end of the day it is an object to be placed where needed. each placement in my mind is a different instantiation of that object. It is just getting to know the nuances of javascript and its global vs local issues, I guess. No biggy. Thanks for the help. – Olsonm76 Mar 30 '22 at 17:30
  • JS doesn't make copies of instances unless you specifically tell it to. – Barmar Mar 30 '22 at 17:38

1 Answers1

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.

Olsonm76
  • 45
  • 1
  • 4