Languages
[Edit]
EN

JavaScript - find intersection of two arrays

0 points
Created by:
Lilly-Grace-Greig
571

In this article, we would like to show you how to find the intersection of two arrays in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const array1 = ['a', 'b'];
const array2 = ['a', 'b', 'c', 'd'];

const result = array1.filter((x) => array2.includes(x));

console.log(result);  // [ 'a', 'b' ]

result:

JavaScript - intersection of two arrays
JavaScript - intersection of two arrays

See also

  1. JavaScript - find difference between two arrays

  2. JavaScript - find symmetric difference of two arrays

  3. JavaScript - detect array items changes (added, updated, removed, unchanged)

References

  1. Array.prototype.filter() - JavaScript | MDN
  2. Array.prototype.includes() - JavaScript | MDN

Alternative titles

  1. JavaScript - get two arrays intersection
  2. JavaScript - find two arrays intersection
  3. JavaScript - array intersection
  4. JavaScript - find matching values in two arrays
  5. JavaScript - calculate intersection of two arrays
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join