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

123
Views
Javascript: Is there a way to check if an array contains at least one element from another array?

Is there a way to test whether or not an array contains at least one element from another array?

let array1 = [`a`, `b`, `c`, `d`, `e`];
let array2 = [`c`];

Basically is there a way to determine if array2 has any of the elements from array1 in a single step. I realize I could check to see if array2 has a or b or c or d or e, but I'm curious if there is a method that helps me.

includes() only allows me to check for one value. some() would allow me to test if c is included in array1 but not test if any of the five elements from array1 are part of the array2.

Is there some way of using reduce() or filter()?

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

0

You could take Array#some with a Set.

const
    array1 = ['a', 'b', 'c', 'd', 'e'],
    array2 = ['c'],
    hasValue = array2.some(Set.prototype.has, new Set(array1));

console.log(hasValue);

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!