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

66
Visualizações
Keep nested array structure the same but attach a property

I have the following nested array which I to preserve and keep the same, but also attach a property called indexes to each nested items object which stores a top-down list of indexes so I can keep track of the nested items when I flatten the array and use it elsewhere in my app. The structure looks like this:

[
    { 
        cat: '1'
        items: [
            {
                media: [
                    {
                        type: 'image'
                    }
                ]
            },
            {
                media: [
                    {
                        type: 'image'
                    }
                ]
            },
            {
                media: [
                    {
                        type: 'image'
                    }
                ]
            }
        ]
    },

    {
        cat: '2',
        items: [
            {
                media: [
                    {
                        type: 'image'
                    },
                    {
                        type: 'image'
                    }
                ]
            }
        ]
    },
    {
        cat: '3',
        items: [
            {
                media: [
                    {
                        type: 'image'
                    }
                ]
            }
        ]
    }
];

And I'd like it to look like the following, where indexes starts at 0 for the first item, and for each occurrence within the media array attaches an incrementing number and binds it as a property.

[
    {
        cat: '1',
        items: [
            {
               indexes: [0],
                media: [
                    {
                        type: 'image'
                    }
                ]
            },
            {
               indexes: [1],
                media: [
                    {
                        type: 'image'
                    }
                ]
            },
            {
               indexes: [2],
                media: [
                    {
                        type: 'image'
                    }
                ]
            }
        ]
    },

    {
        cat: '2',
        items: [
            {
               indexes: [3, 4],
                media: [
                    {
                        type: 'image'
                    },
                    {
                        type: 'image'
                    }
                ]
            }
        ]
    },
    {
        cat: '3',
        items: [
            {
               indexes: [5],
                media: [
                    {
                        type: 'image'
                    }
                ]
            }
        ]
    }
];

What is the best way to do this?

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

0

In my opinion the most elegant way would be to run through all the items using a forEach loop.

const cats = [{
    cat: '1',
    items: [{
        media: [{
          type: 'image'
        }]
      },
      {
        media: [{
          type: 'image'
        }]
      },
      {
        media: [{
          type: 'image'
        }]
      }
    ]
  },

  {
    cat: '2',
    items: [{
      media: [{
          type: 'image'
        },
        {
          type: 'image'
        }
      ]
    }]
  },
  {
    cat: '3',
    items: [{
      media: [{
        type: 'image'
      }]
    }]
  }
];

let totalIndex = 0
cats.forEach(cat => {
  cat.items.forEach(item => {
    item.indexes = item.media.map(() => totalIndex++)
  })
})

console.log(cats)

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