0

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.

oderfla
  • 1,695
  • 4
  • 24
  • 49
  • 2
    You're not returning anything from any of those `map` calls, so they are pretty much useless. Use `filter` if you want to, well, filter an array. – Heretic Monkey Aug 23 '21 at 15:13

1 Answers1

0

Please find array.reduce implementation of same.

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" }
];
const filtrering = [
  { name: 'category', values: ['TV', 'Movies'] },
  { name: 'region', values: ['UK'] }
];

const output = arr.reduce((acc, curr) => {
  let isNodeSatisfied = false;
  filtrering.forEach((criteria) => {
    isNodeSatisfied = isNodeSatisfied || criteria.values.indexOf(curr[criteria.name]) > -1;
  })
  if (isNodeSatisfied) {
    acc.push(curr)
  }
  return acc;
}, []);
console.log(output);
Nitheesh
  • 19,238
  • 3
  • 22
  • 49