I have two objects:
const people = [
{
name: "Dave",
fruit: ["apple", "pear"],
veg: ["asparagus", "peas"]
},
{
name: "Frank",
fruit: ["mango", "banana"],
veg: ["sweetcorn"]
},
{
name: "Alice",
fruit: ["mango", "peach"],
veg: ["asparagus"]
}];
const demographics = {
fruit: ["apple", "mango"],
veg: ["asparagus"]
}
I would like to find which 'people' like the fruit 'apple' or 'mango' and the veg 'asparagus', as defined in the 'demographics' object, like so:
[{
name: "Dave",
fruit: ["apple", "pear"],
veg: ["asparagus", "peas"]
},
{
name: "Alice",
fruit: ["mango", "peach"],
veg: ["asparagus"]
}]
I've been staring at this all day and am not advanced enough to dig into objects like this. Can anyone help? If possible, I would like to use underscore, but not essential.
first I converted the demographics object to an array so i can iterate easily. after that filter the person array with the conditions
check every demographic where in each demographic at least 1 satisfies (is inside the veg or fruit.. arrays of a person).
You can find equivalent methods
pairs,filter,some,every,containsif you want to convert above using underscore