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

200
Visualizações
Expand a nested array to make a flat JS Map?

Say I have a JS Map like this:

[
    {
        id: 1,
        name: "Foo",
        contents: [1,2,3],
        morecontents: ["a","b"],
    },
    {
        id: 2,
        name: "Bar",
        contents: [4,5,6]
    }
]

I want to be able to expand the arrays within it to produce a Map like this:

{
   {
      "id":1,
      "name":"Foo",
      "contents":1,
      "morecontents":"a"
   },
   {
      "id":1,
      "name":"Foo",
      "contents":2,
      "morecontents":"b"
   },
   {
      "id":1,
      "name":"Foo",
      "contents":3,
      "morecontents":""
   }
} 
[...]

Note the arrays will always be at expected keys, and there may be multiple arrays to expand. In the event arrays are different lengths (very likely) the fields can be left blank.

How could this be achieved in Node.JS/vanilla JS?

Thank you.

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

0

array.map(obj =>
    obj.contents.map((content,i) =>
        new Object({
            ...obj,
            contents: content,
            morecontents: obj.morecontents[i] ? obj.morecontents[i] : ""  
        })
).flat()
about 4 years ago · Juan Pablo Isaza Relatório

0

Considering the arrays within the object are dynamic and anyone of these arrays could be of longer length a following solution will be more comprehensive. First it finds the arrays elements within the object, then finds the array with most elements, then it creates copies of object equal to the number of maximum lengths of its child array and lastly updates the elements according to the index of these copies. The results are then flattened into a single result.

            array.map((obj, oi) => {
                const arrayNames = Object.keys(obj).filter(k => Array.isArray(obj[k]));
                let maxlen = arrayNames.map(a => obj[a].length).reduce((p, c) => p > c ? p : c);
                const res = new Array(maxlen).fill({}).map(objn => Object.assign({}, objn, obj));
                res.forEach((x, i) => arrayNames.forEach(name => x[name] = i < obj[name].length ? obj[name][i] : ""));
                return res;
            }).flat()
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