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

175
Vistas
How to filter an array according another array in object?

I have an array:

const arr = [
    {id: 1, text: "hello", likes: [1,2,5]},
    {id: 2, text: "text", likes: []},
    {id: 3, text: "example", likes: [1]}
]

how to filter it according to the length of likes array i.e array should be like this:

const arr = [
        {id: 1, text: "hello", likes: [1,2,5]},
        {id: 3, text: "example", likes: [1]},
        {id: 2, text: "text", likes: []},
    ]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could use Array.prototype.sort() with parameters a,b and compare the length property of the likes property

READ MORE HERE: javascript Array.prototype.sort

const arr = [
    {id: 1, text: "hello", likes: [1,2,5]},
    {id: 2, text: "text", likes: []},
    {id: 3, text: "example", likes: [1]}
]

arr.sort(function(a,b) { 
   return b.likes.length - a.likes.length;
});

for(let a = 0; a < arr.length; a++){
  console.log(arr[a]);
}

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