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

137
Views
How to flatten a array of object having subRows inside?

I have an array of object and have to convert in level 1 json (flatten) it. The keys code and takenBY are dynamic

key1 = "code",
key2 = "takenBy"

const dataObject = [
    {
        "code": "ABCD",
        "subRows": [
            {
                "takenBy": "Ram",
                "workedAccounts": 2,
                "contactableAccounts": 1,
            },
            {
                "takenBy": "krish",
                "workedAccounts": 2,
                "contactableAccounts": 2,
            }
        ],
        "workedAccounts": 4,
        "contactableAccounts": 3,
    },
    {
        "code": "XYZ",
        "subRows": [
            {
                "takenBy": "Jack",
                "workedAccounts": 5,
                "contactableAccounts": 0,
            },
            {
                "takenBy": "krish",
                "workedAccounts": 4,
                "contactableAccounts": 4,
            }
        ],
        "workedAccounts": 9,
        "contactableAccounts": 4,
    },
]

const headerObject = [
    { label: 'Code', field_key: 'code' },
    { label: 'Worked Accounts', field_key: 'workedAccounts' },
    { label: 'Contactable Accounts', field_key: 'contactableAccounts' },
    { label: 'Taken By', field_key: 'takenBy' },
];

const result = [
    {
        "Code": "ABCD",
        "Taken By": "",
        "Worked Accounts": 4,
        "Contactable Accounts": 3,
    },
    {
        "Taken By": "Ram",
        "Worked Accounts": 2,
        "Contactable Accounts": 1,
    },
    {
        "Taken By": "krish",
        "Worked Accounts": 2,
        "Contactable Accounts": 2,
    },
    {
        "Code": "XYZ",
        "Taken By": "",
        "Worked Accounts": 9,
        "Contactable Accounts": 4,
    },
    {
        "Taken By": "Jack",
        "Worked Accounts": 5,
        "Contactable Accounts": 0,
    },
    {
        "Taken By": "krish",
        "Worked Accounts": 4,
        "Contactable Accounts": 4,
    }
];

const result = dataObject .reduce((acc, { subRows, ...rest }) => acc.concat(subRows.map(({ ...props }) => ({ ...props, ...rest}), [])), []);
about 4 years ago · Juan Pablo Isaza
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!