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

170
Views
JavaScript array manipulation interview question

This is a question that I have encountered during an interview:

Write a function to transform the array:

[
  {name:'a',values:[1,2]},
  {name:'b',values:[3]},
  {name:'a',values:[4,5]}
]

to:

[
  {name:'a',values:[1,2,4,5]},
  {name:'b',values:[3]}
]

I know this is not hard, but I just can't figure it out. Does anyone know how to solve it? Are there any places that I can find and practice more practical questions like this one?

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

0

You can group the array by name and then get an array using the grouped object:

const bla = [
  {name:'a',values:[1,2]},
  {name:'b',values:[3]},
  {name:'a',values:[4,5]}
];

const res = Object.values(bla.reduce((obj, { name, values }) => {
  obj[name] = obj[name] ?? {name, values: []}
  obj[name].values.push(...values)
  return obj
}, {}));

console.log(res)

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!