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

197
Visualizações
What's the cleanest way to turn an object into a list without using a double for loop?

I have an object called 'times', that holds another object called '20102', that holds a list of 3 objects. It looks like this:

times: {
    20102: [
        { name:'jane', age:12 },
        { name:'josh', age:19 },
        { name:'jill', age:14 },
    ]
}

However, what I want it to look like is this:

times:[
    { name:'jane', age:12 },
    { name:'josh', age:19 },
    { name:'jill', age:14},
]

I was thinking of doing a double for loop but that's not efficient. What's a better way?

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

0

using Object.values() and flat()

var x = {
  times: {
    20102: [{
        'key': '1'
      },
      {
        'key': '2'
      },
      {
        'key': '3'
      },
    ]
  }
};
x.times = Object.values(x.times).flat();

console.log(x);

If you know there will only be one key

var x = {
  times: {
    20102: [{
        'key': '1'
      },
      {
        'key': '2'
      },
      {
        'key': '3'
      },
    ]
  }
};
x.times = Object.values(x.times)[0];

console.log(x);

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