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

130
Views
Traversing JSON array of objects

I know there are several similar questions on Stack Overflow but despite trying multiple methods, I am still stuck.

I am trying to traverse a JSON array of objects with structure like this

{
"categories": [
    {
        "category_id": "198",
        "category": "Appliances",
        "seo_name": "appliances",
    },
    {
        "category_id": "184",
        "category": "Industrial Appliances",            
        "seo_name": "industrial-appliances"
    },
"params": {
    
    "visible": false,
    "sort_order": "asc",
}

}

I want to get the category and seo-name of each object within categories.

This is what I tried:

fetch(url, {
  method: "GET",
  headers: headers,
})
  .then((response) => response.json())
  .then((json) => { 

    for (let i in json) {
      console.log(json.categories[i].category);
    }
  });

The response I get is:

console.log(json.categories[i].category); 
                               ^ 
TypeError: Cannot read properties of undefined (reading 'category')

How do I get the category and seo-name fields for each object within the categories array?

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

0

Your loop was pretty close - try using the for..of loop:

const json = {
  categories: [
    {
      category_id: "198",
      category: "Appliances",
      seo_name: "appliances",
    },
    {
      category_id: "184",
      category: "Industrial Appliances",
      seo_name: "industrial-appliances",
    },
  ],
  params: {
    visible: false,
    sort_order: "asc",
  },
};


for (let category of json.categories) {
  console.log(category.category + ', ' + category.seo_name)
}

Alternatively, here's how you'd do it with a traditional for-loop:

const json = {
  categories: [
    {
      category_id: "198",
      category: "Appliances",
      seo_name: "appliances",
    },
    {
      category_id: "184",
      category: "Industrial Appliances",
      seo_name: "industrial-appliances",
    },
  ],
  params: {
    visible: false,
    sort_order: "asc",
  },
};


for (let i = 0; i < json.categories.length; i++) {
  const category = json.categories[i]
  console.log(category.category + ', ' + category.seo_name)
}

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!