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

88
Visualizações
Convert into custom object from array of nested array of numbers

I have an array of nested array of numbers and I am trying to convert it into array of object with x, y, z value.

I have the following data

[ 
 [1, 2, 3],
 [2, 3, 4], 
 [4, 5, 6]
]

My expected output is something like this

[ 
  { x: 1, y: 2, z: 3 }, 
  { x: 2, y: 3, z: 4 }, 
  { x: 4, y: 5, z: 6 } 
]

I have done this but I don't think its a standard solution. Any better way anyone can suggest?

data.map((point, i) => {
   return {
     x: point[0],
     y: point[1],
     z: point[2],
  }
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is no better way in the sense of another (quicker) approach of doing this. map() is what you would need to to.

But you could (arguably) improve the readability/ conciseness of the code by using array destructuring and leaving out the explicit return statement. Note that I am also using enhanced object literals.

const input = [ 
 [1, 2, 3],
 [2, 3, 4], 
 [4, 5, 6]
]

const output = input.map(([x, y, z]) => ({x, y, z}));
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Using this approach it is very obvious what the result would be therefore I would prefer this, but - again - your solution is perfectly fine.

about 4 years ago · Juan Pablo Isaza Relatório

0

This would also work. Using array destructuring. This cleans your solution a bit more.

const data = [
  [1, 2, 3],
  [2, 3, 4],
  [4, 5, 6]
]

const output = data.map(([x, y, z]) => ({x, y, z}))

console.log(output)

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