Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

71
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!