Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

149
Visualizações
Beginner confusion on JavaScript syntax regarding nested object function

I ran into an odd problem in this older forum with no explanation for why it works (https://hashnode.com/post/how-to-create-nested-child-objects-in-javascript-from-array-cj9tsc3we01m0uowu4pyuesy0), and while I was able to understand some of the other solutions there that break the problem up into multiple functions, one caught my eye that has royally confused my understanding of some JavaScript concepts. Here is the answer given in question:

let newArray = [1, 2, 3]; //any array to turn into set of nested objects
function arrayToList(insertArray) {
   let list = {};
   for (node = list, i = 0, iterateFor = insertArray.length; i < iterateFor; i++) {
       node = node[insertArray[i]] = {};
   }
   return list;
}
console.log(arrayToList(newArray)); 

With this as the expected output:

{1: {2: {3: { }}}} 

So far I understand everything in the for loop structure, but the actual code block is seems like its missing a line of code. At what point does the node get inserted into the new empty object? My best guess at why this works was the 'node' variable had a different value at the beginning and the end of every iteration. But when I console log the node, it only gives me an empty list: {}

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Logging the node inside the loop will give you an empty object because you're always assigning an empty object to it in the loop. If you're trying to figure things out, log the list instead, because that's the top-level object inside which everything else is assigned.

This:

node = node[insertArray[i]] = {};

does three things: it creates an object, it assigns that object to node[insertArray[i]], and it reassigns the node variable to that new object. A less confusing way of writing it would be:

let newArray = [1, 2, 3]; //any array to turn into set of nested objects
function arrayToList(insertArray) {
   let list = {};
   for (node = list, i = 0, iterateFor = insertArray.length; i < iterateFor; i++) {
       const newNode = {};
       node[insertArray[i]] = newNode;
       node = newNode;
   }
   return list;
}
console.log(arrayToList(newArray));

Now the process should be pretty clear. On every iteration, a new empty object is created, that object is assigned to the most deeply nested object so far in the overall structure, and then the node is assigned to that new empty object (becoming the new most deeply nested object).

about 4 years ago · Juan Pablo Isaza Relatório

0

At what point does the node get inserted into the new empty object?

The variable node does not get inserted into any "new empty object", it is the other way around.

On line 4 of your code, in the for loop, you declare node and assign it as a reference to the pre-existing list object.

Therefore, the first updates to node, e.g. node = node[insertArray[i]] = {}; adds a property "1" to the "parent" list object.

Subsequent iterations of the for loop then update the most recently nested property, referenced by node, with a new empty object.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda