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

128
Views
Convert an object containing more objects into an array

This is what I am working with:

let object = {
  'A':{
    "a": {
        "1": "2",
        "3": "4"},
    "b": {
        "3": "4",
        "5": "6"}
    },
  'B':{
    "c": {
        "7": "8",
        "9": "10"},
    "d": {
        "11": "12",
        "13": "14"}
    }
}

I have been trying to compute my output result to look like this

result = [ 
    {"a": {"1": "2", "3": "4"}},
    {"b": {"3": "4", "5": "6"}},
    {"c": {"7": "8", "9": "10"}}
    {"d": {"11": "12", "13": "14"}}
]

I have tried the following which is closer but not the same as what I am looking for: Object.entries(object).map(([key, value]) => { return value })

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

0

Can be done using 2 forEach

let object = {
  'A':{
    "a": {
        "1": "2",
        "3": "4"},
    "b": {
        "3": "4",
        "5": "6"}
    },
  'B':{
    "c": {
        "7": "8",
        "9": "10"},
    "d": {
        "11": "12",
        "13": "14"}
    }
}



let res = []
Object.values(object).forEach((value) => {
    Object.entries(value).forEach((el) => {
    res.push({[el[0]]: el[1]})
  })
})


console.log(res)

about 4 years ago · Juan Pablo Isaza Report

0

Try this:

Object.values(object)
    .flatMap(a => Object.entries(a))
    .map(([k, v]) => ({ [k]: v }))

Edited to use the last map idea from @Thomas instead of using Object.fromEntries

about 4 years ago · Juan Pablo Isaza Report

0

Try this

let object = {'A':{"a": {"1": "2", "3": "4"},"b": {"3": "4", "5": "6"}},'B':{"c": {"7": "8", "9": "10"},"d": {"11": "12", "13": "14"}}};
let arr = Object.entries(Object.assign({}, ...Object.values(object))).map(([k, v]) => ({[k]: v}));
console.log(arr)

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!