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

168
Views
Get a one-dimensional array from an array of objects

I have the following array of objects:

const input = [
  { key1: 1, key2: 1, key3: 5 },
  { key1: 2, key2: 5, key3: 6 },
  { key1: 4, key2: 6, key3: 7 },
  { key1: 7, key2: 8, key3: 9 },
];

I just want to loop through an array of objects and collect all the values ​​into a one dimensional array like this:

const output = [1, 1, 5, 2, 5, 6, 4, 6, 7, 7, 8, 9];

Can someone help solve this problem?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If the keys are always in that order, you can use Object.values as a callback to flatMap

const output = input.flatMap(Object.values)

const input = [
  { key1: 1, key2: 1, key3: 5 },
  { key1: 2, key2: 5, key3: 6 },
  { key1: 4, key2: 6, key3: 7 },
  { key1: 7, key2: 8, key3: 9 },
];

const output = input.flatMap(Object.values)

console.log(output)


Relying the order of keys in an object is generally NOT a good idea. You can destrcuture all the properties you need and flatten them in that order:

input.flatMap(({ key1, key2,  key3 }) => [key1, key2, key3])

or

Create an array of keys to make it a bit more dynamic:

const keys = ["key1", "key2", "key3"]
const output = input.flatMap(o => keys.map(k => o[k]))
about 4 years ago · Santiago Trujillo 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!