I am looking to arrange an array of objects in a specific order:
The first set should include objects where the favorites array contains only one item. The second set should display objects where the favorites array is either undefined or empty. The third set should consist of objects with more than one item in the favorites array.
Here is how the array of objects is structured:
[
{
rentalName:
rentalAddress:
favorites:[]
...
}
]
I came across a similar solution but it does not cater to the sorting requirement mentioned above:
function sortBy(selector) {
const cmp = (a, b) => (selector(a) - selector(b));
return list => list.sort(cmp);
}
const data = [{ rentalName: "Foo", favorites:[{}, {}, {}] }, { rentalName: "Bar", favorites:[{}] }, { rentalName: "Baz", favoriteslikes:[{}] }, { Name: "Plugh", favorites:[] }];
const sortByLikes = sortBy(({ favorites }) => favorites.length);
console.log(sortByLikes(data));
Is there a way to sort the array of objects into three parts as described earlier?