Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

253
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda