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

115
Views
Checking if an array contains any values from another array?

I have an array of objects that is structured like so

const tools = [
   {name:"React", image:"react.svg"},
   {name:"Firebase", image:"firebase.svg"},
   {name:"CSS", image:"css.svg"},
]

I then have an array of strings.

const arr = ["React", "Firebase"]

How can I check that the 'tools' array includes all of the filters in the 'arr' array?

I think I am supposed to use the array.every() or the array.some() operator, but i am not sure how to implement either one in this case.

I found this function from a duplicate question, but it doesn't exactly fit my use case as the first array is an array of objects.

let checker = (arr, target) => target.every(v => arr.includes(v));

So my assumption is to loop through the 'tools' array and perform the check that way, but that doesn't seem right logic-wise?

If these were 2 straight up arrays of strings I could do a simple includes() operator and be done.

How do I move on from here? I'm considering pulling the individual name properties out of the array into a proxy array, so then it is two arrays of strings, but I feel like that is much less efficient than doing it with some() or every().

Edit: I am not sure I was clear, but I am checking against the 'name' property in the tools array.

Thank you for any guidance.

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

0

You can create a Set from the target array, and then reduce the objects' array (arr) and remove each found item (name in this case) from the accumulator. If the size of the Set is 0, then all targets were found in the array:

const checker = (cb, arr, target) => !arr.reduce((acc, o) => {
  acc.delete(cb(o))
  
  return acc
}, new Set(target)).size

const tools = [{"name":"React","image":"react.svg"},{"name":"Firebase","image":"firebase.svg"},{"name":"CSS","image":"css.svg"}]

console.log(checker(o => o.name, tools, ["React", "Firebase"]))

console.log(checker(o => o.name, tools, ["React", "Firebase", "Cats"]))

With TS types (TS Playground):

const checker = <T>(
  cb: (o: any) => T, 
  arr: any[], 
  target: T[]
): boolean => !arr.reduce((acc: Set<T>, o: any) => {
  acc.delete(cb(o))
  
  return acc
}, new Set(target)).size
about 4 years ago · Juan Pablo Isaza Report

0

A very simple solution to this is to use array.prototpye.some() method. It wilkl

const tools = [
   {name:"React", image:"react.svg"},
   {name:"Firebase", image:"firebase.svg"},
   {name:"CSS", image:"css.svg"},
]

const arr = ["React", "Firebase"]

function findCommon(arr1, arr2){ 
 arr1.forEach(obj => {
   const key = Object.values(obj)
    console.log(key.some(item => arr2.includes(item)))
    }
  )
}

findCommon(tools, arr);

about 4 years ago · Juan Pablo Isaza Report

0

I think the simplest solution would be to use .some():

const tools = [
  {name:"React", image:"react.svg"},
  {name:"Firebase", image:"firebase.svg"},
  {name:"CSS", image:"css.svg"},
];

const arr = ["React", "Firebase"]

const result = arr.every((item) => tools.some((tool) => tool.name === item));

console.log(result); // returns true
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!