What’s the simplest, library-free code for implementing array intersections in javascript? I want to write
intersection([1,2,3], [2,3,4,5])
and get
[2, 3]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Use a combination of
Array.prototype.filterandArray.prototype.includes:For older browsers, with
Array.prototype.indexOfand without an arrow function:NB! Both
.includesand.indexOfinternally compares elements in the array by using===, so if the array contains objects it will only compare object references (not their content). If you want to specify your own comparison logic, useArray.prototype.someinstead.