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

101
Views
How to change object of object of array objects to array objects using javascript

I would like to know how to flatten the array of objects without flap map in javascript

If the of object of arrays object has length greater than 1, flatten

in my example property black has more than object so return that in array of objects

var obj ={
    "details": {
        "black": [
            {
            value: 100,
            name: "xxx"
            },
            {
            value: 200,
            name: "yyy"
            }
        ]
    },
    "sales": {
        "blue": [
            {
               value: 50,
               name: "abc"
            }
        ],
        "ALL": [
            {
              value: 20,
              name: "100"
            }
        ]
    }
}

Expected Output

[
  {
    value: 100,
    name: "xxx"
  },
  {
    value: 200,
    name: "yyy"
  }
]

have tried

const result = Object
    .values(obj)
    .flatMap(v => 
        Object.values(v as any)
            .filter((group: any) => group.length > 1)
    )

without flapmap how to do using javascript

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

0

You can create a recursive flatten function using Array.reduce() that takes a multidimensional array, and converts it to a flat array (TS playground):

const obj = {"details":{"black":[{"value":100,"name":"xxx"},{"value":200,"name":"yyy"}]},"sales":{"blue":[{"value":50,"name":"abc"}],"ALL":[{"value":20,"name":"100"}]}}

const flatten = arr => 
  arr.reduce((acc, a) => acc.concat(
    Array.isArray(a) ? flatten(a) : a
  ), [])

const result = flatten(Object.values(obj)
  .map(v => 
    Object.values(v)
      .filter(group => group.length > 1)
  )
)

console.log(result)

Note: you can also use Array.flat() but since it came out with Array.flatMap() you would probably still have a compatibility problem.

about 4 years ago · Juan Pablo Isaza Report

0

var obj ={
    "details": {
        "black": [
            {
            value: 100,
            name: "xxx"
            },
            {
            value: 200,
            name: "yyy"
            }
        ]
    },
    "sales": {
        "blue": [
            {
               value: 50,
               name: "abc"
            }
        ],
        "ALL": [
            {
              value: 20,
              name: "100"
            }
        ]
    }
}

console.log(obj.details.black)

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!