Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

94
Views
Check if an a propery of an object from an array is contained in another array of object

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);
   }
})
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!