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

116
Views
How to read array object values in a single array

How to retrieve values of objects into a single array.

var arrayOjObjects =  {
  "Fruits": ["Banana", "Apple"],
  "Vegetables": ["Carrot", "Chilli"],
  "Drinks": ["Coco", "Pepsi"]
}
Output = ["Banana", "Apple", "Carrot", "Chilli", "Coco", "Pepsi"]

Tried Object.values(arrayOjObjects) but it is giving multiple arrays. Any inputs on this?

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

0

You could take an array of the keys and then reduce it to the desired result:

var arrayOfObjects = {
  "Fruits": ["Banana", "Apple"],
  "Vegetables": ["Carrot", "Chilli"],
  "Drinks": ["Coco", "Pepsi"]
}

const output = Object.keys(arrayOfObjects).reduce((output, key) => {
  output.push(...arrayOfObjects[key]);
  return output;
}, [])

console.log(output)
//Output = ["Banana", "Apple", "Carrot", "Chilli", "Coco", "Pepsi"]

about 4 years ago · Juan Pablo Isaza Report

0

Use flat, like:

Object.values(arrayOjObjects).flat()

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

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!