
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Taking common elements from many arrays in JavaScript
We are required to write a JavaScript function that takes in any arbitrary number of arrays and returns an array of elements that are common to all arrays. If there are no common elements, then we should return an empty array.
Therefore, let’s write the code for this function −
Example
The code for this will be −
const arr1 = [2, 6, 7, 1, 7, 8, 4, 3]; const arr2 = [5, ,7, 2, 2, 1, 3]; const arr3 = [1, 56, 345, 6, 54, 2, 68, 85, 3]; const intersection = (arr1, arr2) => { const res = []; for(let i = 0; i < arr1.length; i++){ if(!arr2.includes(arr1[i])){ continue; }; res.push(arr1[i]); }; return res; }; const intersectMany = (...arrs) => { let res = arrs[0].slice(); for(let i = 1; i < arrs.length; i++){ res = intersection(res, arrs[i]); }; return res; }; console.log(intersectMany(arr1, arr2, arr3));
Output
The output in the console will be −
[2, 1, 3]
- Related Articles
- JavaScript Program for find common elements in two sorted arrays
- How to find common elements from arrays in android listview?
- C++ Program to find the common elements from two arrays
- Golang Program to find the common elements from two arrays
- Finding the sum of all common elements within arrays using JavaScript
- How to find the common elements between two or more arrays in JavaScript?
- Find common elements in three sorted arrays in C++
- Generating combinations from n arrays with m elements in JavaScript
- intersection_update() in Python to find common elements in n arrays
- How to compare two arrays to see how many same elements they have in JavaScript?
- Java program to find common elements in three sorted arrays
- Python program to find common elements in three sorted arrays?
- C# program to find common elements in three sorted arrays
- Finding the common streak in two arrays in JavaScript
- C# program to find common elements in three arrays using sets

Advertisements