Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda