0

Let's say I have these arrays of object

selectedNames = [
   {label: 'Nicolas', value:'Nicolas'},
   {label: 'Michael', value:'Michael'},
   {label: 'Sean', value:'Sean'},
   {label: 'George', value:'George'}
]

selectedWhatever = [
   {label: 'Guitar', value: 'Guitar'},
   {label: 'Bass', value: 'Bass'}
]

and then another array of objects like this:

list = [
   {name: 'Nicolas', surname: 'Smith', birthplace: 'NewYork', whatever: 'Guitar'},
   {name: 'Sean', surname: 'Narci', birthplace: 'LA', whatever: 'Bass'},
   {name: 'George', surname: 'Rossi', birthplace: 'NewYork', whatever: 'Bass'},
   {name: 'Fiona', surname: 'Gallagher', birthplace: 'Madrid', whatever: 'Drum'},
   {name: 'Michael', surname: 'Red', birthplace: 'London', whatever: 'Triangle'},
]

and I want to filter list based on the datas I have in the others two arrays and group by birthplace, so I want this:

result = {
   LA: [
      {name: 'Sean', surname: 'Narci', birthplace: 'LA', whatever: 'Bass'},
   ],
   NewYork: [
      {name: 'Nicolas', surname: 'Smith', birthplace: 'NewYork', whatever: 'Guitar'},
      {name: 'George', surname: 'Rossi', birthplace: 'NewYork', whatever: 'Bass'},
   ]
}

What I've done is the following and it works fine. Is there a smarter or more elegant way to do the same?

const obj = {};

list.map((res) => {
   if ((data.selectedNames.map((r) => r.label).includes(res.name))
      && (data.selectedWhatever.map((r) => r.label).includes(res.whatever))
   ){
      result[res.birthplace] = result[res.birthplace] ?? []:
      result[res.birthplace].push(res);
   }
})
palnic
  • 386
  • 1
  • 4
  • 13
  • 1
    uses `list.forEach` instead of `list.map`, uses `data.selectedNames.filte(r => r.label === res.name)r` instead of `data.selectedNames.map((r) => r.label).includes(res.name))` (and same change for `data.selectedWhatever.map((r) => r.label).includes(res.whatever)`) – Sphinx Sep 16 '21 at 21:52

1 Answers1

0

Instead of creating and searching a new array of names and whatever for every checked entry, one could create those only once and store them together with the key to he checked as Sets:

 const filters = {
    name: new Set( selectedNames.map(it => it.label) )
 };

 const result = list.filter(it => 
    Object.entries(filters).some(([key, set]) =>
        set.has( it[key] )
     )
  );

The grouping part should really also be a reusable function.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151