JSFiddle - React, Tailwind, and code Playground
HTML
<pre id="out"></pre>
JavaScript
var peoples = [{
'people': {
'sex': 'male',
'diet': 'vegetarian',
'favPlaces': ['place1', 'place2']
}
}, {
'people': {
'sex': 'female',
'diet': 'flexitarian',
'favPlaces': ['place2', 'place3']
}
}, {
'people': {
'sex': 'male',
'diet': 'flexitarian',
'favPlaces': ['place4', 'place1']
}
}];
var filterObj = {
'sex' : ['male'],
'diet': []
};
var matchingSet = peoples.filter(function(p) {
return (filterObj.sex.length === 0 || filterObj.sex.indexOf(p.people.sex) !== -1) //peoples' sex must exists in the filter.sex
&& (filterObj.diet.length === 0 || filterObj.diet.indexOf(p.people.diet) !== -1); //and people's diet must exists in the filter.diet
});
console.log(matchingSet);
var matchingPlaces = matchingSet.map(function(p) { return p.people.favPlaces; });
var flatten = _.chain(matchingPlaces)
.flatten()
.uniq()
.sortBy(function(p) { return p; })
.value();
console.log(flatten);
document.querySelector('#out').innerHTML = JSON.stringify(flatten);