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

135
Views
How to loop through a JSON like this with javascript?

I want to loop through a JSON like this using javascript.

{
    persons: [
      Person {
        name: 'Herbert',
        age: 70
      },
      Person {
        name: 'Peter',
        age: 67
      }
    ]
  }

My first approach was something like this, but this is not working somehow :(

  personArray.forEach((person) => {
    console.log(person.name);
  });
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You may use this to get name:

let personArray = {
    persons: [
      {
        name: 'Herbert',
        age: 70
      },
      {
        name: 'Peter',
        age: 67
      }
    ]
}

personArray.persons.map(ele => ele.name) // ['Herbert', 'Peter']
about 4 years ago · Santiago Trujillo Report

0

JSON does not have forEach, you need to do like this:

let personArray = {
    persons: [
    {
        name: 'Herbert',
        age: 70
    },
    {
        name: 'Peter',
        age: 67
    }
  ]
}
Object.keys(personArray.persons).forEach((key) => {
    console.log(personArray.persons[key].name);
});

about 4 years ago · Santiago Trujillo Report

0

Your json looks invalid. If I understood you correctly, you can do the following:

let json = '{"persons":[{"name":"Herbert","age":70},{"name":"Peter","age":67}]}'
let personArray = JSON.parse(json)

personArray.persons.forEach((person) => {
  console.log(person.name);
});

or, if it is a real javascript object, do the following:


let personArray = {
    persons: [
      {
        name: 'Herbert',
        age: 70
      },
      {
        name: 'Peter',
        age: 67
      }
    ]
}



personArray.persons.forEach((person) => {
  console.log(person.name);
});
about 4 years ago · Santiago Trujillo 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!