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

170
Visualizações
Get a one-dimensional array from an array of objects

I have the following array of objects:

const input = [
  { key1: 1, key2: 1, key3: 5 },
  { key1: 2, key2: 5, key3: 6 },
  { key1: 4, key2: 6, key3: 7 },
  { key1: 7, key2: 8, key3: 9 },
];

I just want to loop through an array of objects and collect all the values ​​into a one dimensional array like this:

const output = [1, 1, 5, 2, 5, 6, 4, 6, 7, 7, 8, 9];

Can someone help solve this problem?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If the keys are always in that order, you can use Object.values as a callback to flatMap

const output = input.flatMap(Object.values)

const input = [
  { key1: 1, key2: 1, key3: 5 },
  { key1: 2, key2: 5, key3: 6 },
  { key1: 4, key2: 6, key3: 7 },
  { key1: 7, key2: 8, key3: 9 },
];

const output = input.flatMap(Object.values)

console.log(output)


Relying the order of keys in an object is generally NOT a good idea. You can destrcuture all the properties you need and flatten them in that order:

input.flatMap(({ key1, key2,  key3 }) => [key1, key2, key3])

or

Create an array of keys to make it a bit more dynamic:

const keys = ["key1", "key2", "key3"]
const output = input.flatMap(o => keys.map(k => o[k]))
about 4 years ago · Santiago Trujillo 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