Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

203
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda