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

165
Views
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 answers
Answer question

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 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!