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

124
Views
Filtering array A based on whether it contains any or partial elements from array B

I have two arrays, array A has a list of elements that may or may not contains an element from array B. I want to filter array A so that it only contains elements where it contains at least one element from array B.

const a = [
  'cqAWS1x2', 
  'cqAM1c',
  'cqA10a + cqA10b',
  'cqA10axZAF + cqA10bxZAF']

const b = [
  'cqAWS1x2',
  'cqA10a',
  'cqA10axZAF']

I want to filter a so it'll be

 ['cqAWS1x2',
  'cqA10a + cqA10b',
  'cqA10axZAF + cqA10bxZAF']

'cqAWS1x2' because Array b contains that element, 'cqA10a + cqA10b' because b contains 'cqA10a' which is part of 'cqA10a + cqA10b'. Same for 'cqA10axZAF + cqA10bxZAF', because b contains 'cqA10axZAF'.

This is what I have so far:

a.filter(avc => b.some(bvc => bvc.includes(avc)))

In the result, I get only ['cqAWS1x2'] but not 'cqA10a + cqA10b' and 'cqA10axZAF + cqA10bxZAF'.

Thanks for all the help!

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

0

You just need to switch the avc and bvc in your includes() call (avc is the longer string, check if it includes the shorter bvc).

const a = ['cqAWS1x2', 'cqAM1c', 'cqA10a + cqA10b', 'cqA10axZAF + cqA10bxZAF'];
const b = ['cqAWS1x2', 'cqA10a', 'cqA10axZAF'];

console.log(a.filter((avc) => b.some((bvc) => avc.includes(bvc))));

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!