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}]
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)