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

96
Views
Transforming a complex object into an array

I am creating a React app using data from an API. the response data is a long complicated object that looks something like this:

{
    "Frozen Products": [
        {
            "_id": "6181849285e8d8f86be2d9df",
            "name": "Peas (800g)",
            "category_id": "6181841060c425f76e57b603",
            "slug": "vegetables",
            "quantity": 16,
            "price": {
                "amount": 3.00
            },
            "thumbnail":"URI ...",,
            "images": [
                "URI ...",
            ],
            "synonyms": [
                "Veggies"
            ],
        },

//etc for many key/values inside this object ..
        
}

I need to access the information do display a category ("Frozen Products" in this case) and all the product names and images under it.

But I think I need to access each Key/Value pair by loopin through their index (example data[0]) not the Key name since they keys are dynamically created by an API. So how can I change this to:

{
   [ "Frozen Products": [
        {
            "_id": "6181849285e8d8f86be2d9df",
            "name": "Peas (800g)",
            "category_id": "6181841060c425f76e57b603",
            "slug": "vegetables",
            "quantity": 16,
            "price": {
                "amount": 3.00
            },
            "thumbnail":"URI ...",,
            "images": [
                "URI ...",
            ],
            "synonyms": [
                "Veggies"
            ],
        },
   ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

you can loop through an object with for in iterator. No matter what key names are

for(key in object) {
  let value = object[key];
}
about 4 years ago · Juan Pablo Isaza Report

0

I think you need something like this:

let data = {
    "Frozen Products": [
        {
            "_id": "6181849285e8d8f86be2d9df",
            ...
        }
    ],
    "Another Category": [...]    
}

let categories = Object.keys(data).map((key) => ({categoryName: key, products: data[key]}));
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!