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

125
Visualizações
creating an multidimensional Object dynamically from an multidimensional array

Im trying to create an multidimensional Object like this one:

{ A : {a1: {},a2:{}}, B: {b1:{},b2:{}}}

from an multidimensional array like this one:

let array1 = [
      ['A', 'a1'],
      ['A', 'a1'],
      ['A', 'a2'],
      ['B', 'b1'],
      ['B', 'b1'],
      ['B', 'b2'],
    ];

I'm trying this for some hours now and was also reading plenty of entrys here on stackoverflow, but nothing really fits this specific case.

What i did so far:

    let array1 = [
      ['A', 'a1'],
      ['A', 'a1'],
      ['A', 'a2'],
      ['B', 'b1'],
      ['B', 'b1'],
      ['B', 'b2'],
    ];

    let object1 = {};

    array1.forEach(function (subArray) {
      let level1 = subArray[0];
      let level2 = subArray[1];

      object1[[level1]] = { ...{ [level2]: {} } };
    });

    console.log('object: ', object1);
    //desired output:  object = { A : {a1: {},a2:{}}, B: {b1:{},b2:{}}}
    //what I get:  object = { A : {a2:{}}, B: {b2:{}}}

So somehow in my code the entrys like {a1: {}} are getting overwritten in each iteration instead of adding a new entry.

Thanks a lot in advance.

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

0

You can use Array.reduce() along with some destructuring to get the required object output.

For each key, value pair in the original array, we add a property to the final object, (e.g. 'A'), then add a new object for each value in the array (e.g. 'a1', 'a2').

let array1 = [ ['A', 'a1'], ['A', 'a1'], ['A', 'a2'], ['B', 'b1'], ['B', 'b1'], ['B', 'b2'], ]; 

let output = array1.reduce((acc, [k,v]) => {
    acc[k] = { ...(acc[k] || {}), [v]: {} };
    return acc;
}, {})

console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }

One could also do this in a one-liner, but I think it's a lot harder to read (and understand):

let array1 = [ ['A', 'a1'], ['A', 'a1'], ['A', 'a2'], ['B', 'b1'], ['B', 'b1'], ['B', 'b2'], ]; 
let output = array1.reduce((acc, [k,v]) => ({ ...acc, [k]: { ...(acc[k] || {}), [v]: {} } }), {});

console.log(output)
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Keep previous properties using ...object1[[level1]]:

    let array1 = [
      ['A', 'a1'],
      ['A', 'a1'],
      ['A', 'a2'],
      ['B', 'b1'],
      ['B', 'b1'],
      ['B', 'b2'],
    ];

    let object1 = {};

    array1.forEach(function (subArray) {
      let level1 = subArray[0];
      let level2 = subArray[1];

      object1[[level1]] = {
        ...object1[[level1]], // Keep previous properties
        ...{ [level2]: {} } // Add new
      };
    });

    console.log('object: ', object1);

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