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

245
Views
How to convert an array of objects into key value pairs
 var contacts = [
    { account: "Acme", firstName: "John", lastName: "Snow" },
    { account: "Metal Industries", firstName: "Ted", lastName: "Smith" },
    { account: "Acme", firstName: "Sara", lastName: "Butler" },
    { account: "HiTech Corp", firstName: "Sam", lastName: "Johnson" },
    { account: "HiTech Corp", firstName: "Arnold", lastName: "Williams" },
    { account: "Metal Industries", firstName: "Jessica", lastName: "Westcoat" },
    { account: "Acme", firstName: "Kyle", lastName: "Johnson" },
    { account: "HiTech Corp", firstName: "Jason", lastName: "Fernandez" }
  ];

The goal is to get this output:

  result =  {
    "Acme": ["John Snow", "Kyle Johnson", "Sara Butler"],
    "HiTech Corp": ["Arnold Williams", "Jason Fernandez", "Sam Johnson"],
    "Metal Industries": ["Jessica Westcoat", "Ted Smith"]
  }

My function below is not returning the array of values and only returns the last value

  const convertArrayToObject = (array, key) => {
    const initialValue = {}
    return array.reduce((obj, item) => {
        return {...obj,[item[key]]: item,}
    }, initialValue)
  }

Output

Any help is appreciated

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

 var contacts = [
    { account: "Acme", firstName: "John", lastName: "Snow" },
    { account: "Metal Industries", firstName: "Ted", lastName: "Smith" },
    { account: "Acme", firstName: "Sara", lastName: "Butler" },
    { account: "HiTech Corp", firstName: "Sam", lastName: "Johnson" },
    { account: "HiTech Corp", firstName: "Arnold", lastName: "Williams" },
    { account: "Metal Industries", firstName: "Jessica", lastName: "Westcoat" },
    { account: "Acme", firstName: "Kyle", lastName: "Johnson" },
    { account: "HiTech Corp", firstName: "Jason", lastName: "Fernandez" }
  ];

const convertArrayToObject = (array, key) => {
    const initialValue = {}
    array.forEach((obj, item) => {
        initialValue[obj[key]] = initialValue[obj.account] || []
        initialValue[obj[key]].push(`${obj.firstName} ${obj.lastName}`)
    })
    return initialValue
}

console.log(convertArrayToObject(contacts, 'account'))
about 4 years ago · Juan Pablo Isaza Report

0

You were close, just missing that when you set up the object in the resultant array, you need to set it as an array - in this script that would be [k]: [v] and not [k]: v

var contacts = [
    { account: "Acme", firstName: "John", lastName: "Snow" },
    { account: "Metal Industries", firstName: "Ted", lastName: "Smith" },
    { account: "Acme", firstName: "Sara", lastName: "Butler" },
    { account: "HiTech Corp", firstName: "Sam", lastName: "Johnson" },
    { account: "HiTech Corp", firstName: "Arnold", lastName: "Williams" },
    { account: "Metal Industries", firstName: "Jessica", lastName: "Westcoat" },
    { account: "Acme", firstName: "Kyle", lastName: "Johnson" },
    { account: "HiTech Corp", firstName: "Jason", lastName: "Fernandez" }
  ];


const convertArrayToObject = (array, key) => {
  const initialValue = {}
  return array.reduce((obj, item) => {
    let k = item[key], v=item.firstName + ' ' + item.lastName;
    if (obj[k]) obj[k].push(v);
    else obj = { ...obj, [k]: [v] }
    return obj
  }, initialValue)
}

console.log(convertArrayToObject(contacts, 'account'))

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!