Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

73
Vistas
Object values to map

I want to map specific properties from an array of objects into another array of objects while using a value as a key in the new object.

For example, I have this object (the syntax doesn't matter is just pseudo-code):

const obj= [
  {id:1 , height: 57 ,name:'jhon' , state: 'true', time: ''},
  {id:2 , height:64 ,name:'max' , state: 'true', time: ''},
  {id:3 , height:62 ,name:'tim' , state: 'false', time: ''},
  {id:4 , height:510 ,name:'alex' , state: 'false', time: ''},
  {id:5 , height: 510 ,name:'frank' , state: 'true', time: ''}
]

And I want to make a map with the name as the key and height as the value, so it looks like this:

[
  {jhon : 57},
  {max : 64},
  ...
]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You can use Array.map with parameter destructuring and computed property name.

obj.map(({ name, height }) => ({ [name]: height }))

const obj = [
  { id: 1, height: 57, name: 'jhon', state: 'true', time: '' },
  { id: 2, height: 64, name: 'max', state: 'true', time: '' },
  { id: 3, height: 62, name: 'tim', state: 'false', time: '' },
  { id: 4, height: 510, name: 'alex', state: 'false', time: '' },
  { id: 5, height: 510, name: 'frank', state: 'true', time: '' }
]

console.log(obj.map(({ name, height }) => ({ [name]: height })))

about 4 years ago · Juan Pablo Isaza Denunciar

0

I'm setting the name as a key using "[]" and returning a object with the height

const obj = [
  { id: 1, height: 57, name: 'jhon', state: 'true', time: '' },
  { id: 2, height: 64, name: 'max', state: 'true', time: '' },
  { id: 3, height: 62, name: 'tim', state: 'false', time: '' },
  { id: 4, height: 510, name: 'alex', state: 'false', time: '' },
  { id: 5, height: 510, name: 'frank', state: 'true', time: '' }
]

const result = obj.map(user => {
  return {[user.name]: user.height}
})

console.log(result);
about 4 years ago · Juan Pablo Isaza Denunciar

0

you can also use reduce

const obj= [
    {id:1 , height: 57 ,name:'jhon' , state: 'true', time: ''},
    {id:2 , height:64 ,name:'max' , state: 'true', time: ''},
    {id:3 , height:62 ,name:'tim' , state: 'false', time: ''},
    {id:4 , height:510 ,name:'alex' , state: 'false', time: ''},
    {id:5 , height: 510 ,name:'frank' , state: 'true', time: ''},
    ]

    const newObj = obj.reduce((accu,curr)=>{
        const ob = {
            [curr.name]:curr.height
        }
        accu.push(ob)
        return accu
    },[])

    console.log(newObj)

console.log(newObj)
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda