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

114
Views
How to Reorder/Move certain elements in an array to the initial indexes based on a Boolean variable in JavaScript

Is there any way to filter this below array such that, the objects which has isTop:true has to be on the initial indexes of the array. Actual Array:

[{id:'1',name:'CAR',isTop:false},
{id:'2',name:'BIKE',isTop:true},
{id:'3',name:'TRUCK',isTop:false},
{id:'4',name:'BUS',isTop:false},
{id:'5',name:'VAN',isTop:true},
{id:'6',name:'TRAIN',isTop:false}]

Result Array I need:

[{id:'2',name:'BIKE',isTop:true},
{id:'5',name:'VAN',isTop:true},
{id:'3',name:'TRUCK',isTop:false},
{id:'4',name:'BUS',isTop:false},
{id:'1',name:'CAR',isTop:false},
{id:'6',name:'TRAIN',isTop:false}]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You could use array.sort.

In my solution, you could use the idea of true - false = 1 to do the sort

let array = [{id:'1',name:'CAR',isTop:false},
{id:'2',name:'BIKE',isTop:true},
{id:'3',name:'TRUCK',isTop:false},
{id:'4',name:'BUS',isTop:false},
{id:'5',name:'VAN',isTop:true},
{id:'6',name:'TRAIN',isTop:false}]
    
    array.sort((x, y) =>(y.isTop-x.isTop))
    console.log(array)

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!