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

133
Views
Filtering an object array with another array

I am having a filtering problem..

objArray is the array that needs to be filtered. selectedNames is an array that contains the values that I want to find in objArray. I need to fetch all objects that have one or more values from selectedNames in their "names" property (an array) .

The output I am trying to get is :

let result = [{names:["A","B","C","D"]},
              {names:["A","B"]},
              {names:["A","D"]}
             ]

Here is a simplified version of my code:

let objArray = [{names:["A","B","C","D"]},
                {names:["C","D"]},
                {names:["C","D","E"]},
                {names:["A","B"]},
                {names:["A","D"]}
               ]

let selectedNames = ["A","B"]

result = this.objArray .filter(obj => {
   return this.selectedNames.includes(obj.names)
}


My code seems to work fine if names attribute was a single value and not an array. But I can't figure out how to make it work on an array.

Any help is more than welcome

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could do something like this. Filtering the array based on the names property having 'some' value be included in the selectedNames array. ...

objArray.filter(obj => obj.names.some(name => selectedNames.includes(name)));

[Edit] As @RadicalTurnip pointed out, this is not performant if the selectedNames is too large. I would suggest that you use an object. E.x

...
const selectedNamesMap = selectedNames.reduce((p,c) => ({...p, [c]: true}), {});
objArray.filter(obj => obj.names.some(name => selelectedNamesMap[name]));

Overkill, but if the arrays are really large (millions of elements) then you are better of using regular for loop and not array methods.

about 4 years ago · Juan Pablo Isaza Report

0

This result is not performant scaled up, but I don't know that there is any way to ensure that it will be performant unless you know more information (like the names are already sorted). That being said, you just missed one more piece of logic.

result = this.objArray.filter(obj => {
  let toReturn = false;
  obj.names.forEach(name => {
    if (this.selectedNames.includes(name))
      toReturn = true;
    };
  };
  return toReturn;
};
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!