If I have two objects, and they both have two array fields within them, like so:
const list1 = {
name: 'list-1',
fruits: ['banana', 'strawberry', 'cherry'],
vegs: ['lettuce', 'avocado', 'beans']
};
const list2 = {
name: 'list-2',
fruits: ['banana', 'apple', 'orange', 'watermelon'],
vegs: ['potato', 'avocado', 'onion', 'cabbage']
};
And then, I pass in two arrays, one of fruits, and one of vegetables, e.g.:
const fruits = ['banana', 'strawberry'];
const vegetables = ['potato', 'lettuce', 'avocado'];
How can I order the objects, so that has the one with most number of fruits and vegetables (based on the passed in arrays) is on top?
In this case that'd be list1
, since it has both "banana" and "strawberry" in fruits, and also has "lettuce" and "avocado" in vegs (i.e. 4 matches in total), whereas list2
only has 2 hits in total.
Not sure if that makes a lot of sense, but what would be the most efficient way to order the two objects based on the arrays?