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

134
Vistas
Ordenar matriz de objetos por valor de propiedad dentro de una matriz anidada de objetos

Estoy tratando de averiguar cómo ordenar una matriz de objetos según si el valor de una propiedad en una matriz anidada de objetos contiene el valor stopped . Cuando ese valor existe en cualquier matriz anidada de objetos, necesito que el objeto principal se ordene en la parte superior, desde allí, estoy tratando de ordenar secundariamente esa lista ordenada por id .

 const arr = [{ id: 1, things: [{ thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, ], }, { id: 2, things: [{ thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, { thing_id: 1, status: 'stopped', }, ], }, { id: 3, things: [{ thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, ], } ] // desired result [{ id: 2, things: [{ thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, { thing_id: 1, status: 'stopped', }, ], }, { id: 1, things: [{ thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, ], }, { id: 3, things: [{ thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, { thing_id: 1, status: 'started', }, ], } ]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

arr.sort((a, b) => { const stoppeds_in_a = a.things.map(obj => obj.status).filter(status => status === 'stopped').length const stoppeds_in_b = b.things.map(obj => obj.status).filter(status => status === 'stopped').length // I want that who has more 'stoppeds' occurrences first return stoppeds_in_b - stoppeds_in_a })
about 4 years ago · Juan Pablo Isaza Denunciar

0

 const checkStopped = (things) => things.some((el) => el.status === 'stopped'); const desired = arr.sort((a, b) => checkStopped(b.things) - checkStopped(a.things));

about 4 years ago · Juan Pablo Isaza Denunciar

0

arr.sort((a, b) => { if (a.things.some(thing => thing.status === "stopped")) { return -1; } else { return a.id - b.id; } });

Simplemente tiene que ordenar, verificando que el objeto actual que se está inspeccionando tenga al menos una "cosa" con un estado "detenido", de lo contrario, un orden numérico normal.

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