I have a collection (list) of objects like below,
books{
book1[
isSelected = false;
/*other properties*/
],
book2[
isSelected = true;
/*other properties*/
],
book3[
isSelected = false;
/*other properties*/
]
}
I want to iterate over this list and print ALL the objects which are having property isSelected
as true
followed by ALL the objects having property isSelected
as false
.
Please guide me on this approach.
A simple solution in vanilla JS is to sort the array by the property:
books.sort(function(a, b) {
return b.isSelected - a.isSelected
})
An underscore way would be groupBy
:
r = _.groupBy(books, 'isSelected')
console.log(r[true]);
console.log(r[false]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.