0

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.

1 Answer 1

2

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]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.