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

202
Views
How can I loop through this array of objects in javascript?

I have an array of objects here:

const users = [
    {
        name: 'xyz',
        claims: [
            {"location": 'dallas',
            "id": '123'},
            {"location": 'frisco',
            "id": '123'},
            {"location": 'plano',
            "id": '123'},
        ]
    },
    {
        name: 'abc',
        claims: [
            {"location": 'richardson',
            "id": '123'},
            {"location": 'rowlett',
            "id": '123'},
            {"location": 'dallas',
            "id": '123'},
        ]
    },
    {
        name: 'adr',
        claims: [
            {"location": 'frisco',
            "id": '123'},
            {"location": 'irving',
            "id": '123'},
            {"location": 'dallas',
            "id": '123'},
        ]
    }
]

I want to get all the locations with dallas as the value. How can I do that? I know I would probably use a loop (maybe a forEach) but I am not exactly sure how I could get the values that I need. Would I use filter?

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

0

Use flatMap with combination of filter:

const dallasLocations = users.flatMap(user => {
    return user.claims.filter(claim => claim.location === 'dallas')
})
about 4 years ago · Juan Pablo Isaza Report

0

You can use flatMap to flatten the mapped array after filtering the locations which equal to dallas.

const users = [
    {
        name: 'xyz',
        claims: [
            {"location": 'dallas',
            "id": '123'},
            {"location": 'frisco',
            "id": '123'},
            {"location": 'plano',
            "id": '123'},
        ]
    },
    {
        name: 'abc',
        claims: [
            {"location": 'richardson',
            "id": '123'},
            {"location": 'rowlett',
            "id": '123'},
            {"location": 'dallas',
            "id": '123'},
        ]
    },
    {
        name: 'adr',
        claims: [
            {"location": 'frisco',
            "id": '123'},
            {"location": 'irving',
            "id": '123'},
            {"location": 'dallas',
            "id": '123'},
        ]
    }
]

const result = users.flatMap(u => u.claims.filter(c => c.location === 'dallas'))

console.log(result)

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!