Find the common elements of more than two JavaScript arrays ?

Given an HTML document having multiple arrays with some elements and the task is to get the common elements from arrays with the help of JavaScript. There are two approaches that are discussed below.
Approach 1: First get the arrays in 2-dimensional format then take the first array by shift() method and then filter out the elements from the first array which are common in all by using filter() method. The elements which are common in all arrays can be checked by every() method and if the element of first array matches with all elements then that element is returned.
- Example:
<!DOCTYPE HTML><html>ÂÂ<head>   Â<title>       ÂFinding matches between more than       Â2 JavaScript arrays   Â</title>   Â<style>       Âbody {           Âtext-align:center;       Â}       Âh1 {           Âcolor: green;       Â}       Â#zambiatek {           Âfont-size: 16px;           Âfont-weight: bold;       Â}        Â       Â#gfg {           Âcolor: green;           Âfont-size: 18px;           Âfont-weight: bold;       Â}   Â</style></head>ÂÂ<bodystyle="">   Â<h1>           Âzambiatek       Â</h1>   Â<pid="zambiatek">   Â</p>   Â<buttononClick="GFG_Fun()">       Âclick here   Â</button>   Â<pid="gfg">   Â</p>   Â<script>       Âvar up = document.getElementById('zambiatek');       Âvar down = document.getElementById('gfg');       Âvar arr1 = [1, 3, 5, 7, 9];       Âvar arr2 = [1, 2, 4, 5, 7];       Âvar arr3 = [1, 2, 5, 7, 8];       Âvar arr = [arr1, arr2, arr3];       Âup.innerHTML =           Â"Click on button to get the common elements from these"+           Â" array<br>Array1 - [" + arr1 + "]<br>Array2 -"+           Â" [" + arr2 + "]<br>Array3 - [" + arr3 + "]";       Âfunction GFG_Fun() {           Âarr4 = arr.slice();           Âdown.innerHTML = arr4.shift().filter(function(v) {               Âreturn arr4.every(function(a) {                   Âreturn a.indexOf(v) !== -1;               Â});           Â});       Â}   Â</script></body>ÂÂ</html>
- Output:
Approach 2: First get the arrays in 2-dimensional format then use the reduce() method to get the access to all arrays one by one. variable(p) contains each array one by one and returns only elements of the array which are in variable(c) array by using filter() method. At the end variable(c) contains the array of common elements.
- Example: 
<!DOCTYPE HTML><html>ÂÂ<head>   Â<title>       ÂFinding matches between more than       Â2 JavaScript arrays   Â</title>   Â<style>       Âbody {           Âtext-align:center;       Â}       Âh1 {           Âcolor: green;       Â}       Â#zambiatek {           Âfont-size: 16px;           Âfont-weight: bold;       Â}        Â       Â#gfg {           Âcolor: green;           Âfont-size: 18px;           Âfont-weight: bold;       Â}   Â</style></head>ÂÂ<bodystyle="">   Â<h1>    Âzambiatek   Â</h1>   Â<pid="zambiatek">   Â</p>   Â<buttononClick="GFG_Fun()">       Âclick here   Â</button>   Â<pid="gfg">   Â</p>   Â<script>       Âvar up = document.getElementById('zambiatek');       Âvar down = document.getElementById('gfg');       Âvar arr1 = [1, 3, 5, 7, 9];       Âvar arr2 = [1, 2, 4, 5, 7];       Âvar arr3 = [1, 2, 5, 7, 8];       Âvar arr = [arr1, arr2, arr3];       Âup.innerHTML =           Â"Click on button to get the common elements from these"+           Â" array<br>Array1 - [" + arr1 + "]<br>Array2 -"+           Â" [" + arr2 + "]<br>Array3 - [" + arr3 + "]";       Âfunction GFG_Fun() {           Âdown.innerHTML = arr.reduce((p, c) =>           Âp.filter(e => c.includes(e)));       Â}   Â</script></body>ÂÂ</html>
- 
Output:
 
				 
					 



