I have an array of objects that looks like this:
const arr = [
{name: "1", category: "TV", region: "US"}
,{name: "2", category: "TV", region: "SE"}
,{name: "3", category: "Movies", region: "US"}
,{name: "4", category: "Music", region: "US"}
,{name: "5", category: "Movies", region: "UK"}
,{name: "6", category: "Movies", region: "UK"}
,{name: "6", category: "Cartoon", region: "SP"}
];
Please note that no field is unique. As you see, there can be mutiple name:6. However, the objects itself is unique, because no object has exactly the same properties as another one.
I want to apply the OR criterium. So I want all the objects that match at least with one of the properties. For example, with this json array with criteria :
const filtrering = [
{ name: 'category', values: [ 'TV', 'Movies' ] }
,{ name: 'region', values: [ 'UK' ] }
];
I expect to see an array with all the values but the number 4 (which has category=Music and region=US, which are not in the criteria array). Using this tripple mapping, I get the right objects, but with duplicates:
filtrering.map(filter => {
arr.map(ind => {
filter.values.map(vl => {
if(ind[filter.name] === vl){
console.log("index found:",ind);
}
});
});
});
So the console is logging the following:
index found: { name: '1', category: 'TV', region: 'US' }
index found: { name: '2', category: 'TV', region: 'SE' }
index found: { name: '3', category: 'Movies', region: 'US' }
index found: { name: '5', category: 'Movies', region: 'UK' }
index found: { name: '6', category: 'Movies', region: 'UK' }
index found: { name: '5', category: 'Movies', region: 'UK' }
index found: { name: '6', category: 'Movies', region: 'UK' }
Maybe I could just remove duplicates. But to me this does not look like the best algorithm to solve this issue.