Home > Web Front-end > JS Tutorial > How to Find Matching Elements Across Multiple JavaScript Arrays?

How to Find Matching Elements Across Multiple JavaScript Arrays?

Patricia Arquette
Release: 2024-10-24 18:45:09
Original
897 people have browsed it

How to Find Matching Elements Across Multiple JavaScript Arrays?

Matching Elements Across Multiple JavaScript Arrays

Problem

Given multiple JavaScript arrays of string values, find the elements that occur identically in all the arrays. For example:

<code class="javascript">var arr1 = ['apple', 'orange', 'banana', 'pear', 'fish', 'pancake', 'taco', 'pizza'];
var arr2 = ['taco', 'fish', 'apple', 'pizza'];
var arr3 = ['banana', 'pizza', 'fish', 'apple'];</code>
Copy after login

Expected output:

<code class="javascript">['apple', 'fish', 'pizza']</code>
Copy after login

Solution

To find the matching elements, we can use a combination of Array methods:

<code class="javascript">const arrays = [arr1, arr2, arr3];
const result = arrays.shift().filter((v) => {
  return arrays.every((a) => {
    return a.indexOf(v) !== -1;
  });
});</code>
Copy after login

Steps:

  1. Use arrays.shift() to remove the first array from the original list.
  2. Use .filter() on the first array to loop through each element v.
  3. Use .every() on the rest of the arrays to check if each one contains the current element v.
  4. If the element v is present in every array, include it in the result array.

By iteratively applying this logic to each array, we can effectively find the elements that occur identically in all the provided arrays.

Additional Considerations

Duplicate Element Handling:

The provided solution assumes that the arrays do not contain duplicate elements. If they do, you can modify the code as follows:

<code class="javascript">const result = arrays.shift().reduce((res, v) => {
  if (res.indexOf(v) === -1 &amp;&amp; arrays.every((a) => {
    return a.indexOf(v) !== -1;
  })) res.push(v);
  return res;
}, []);</code>
Copy after login

This code uses .reduce() instead of .filter(). It checks for duplicates within the result array and only adds unique elements that are present in every array.

Arbitrary Number of Arrays:

The solution is designed to work with an unknown number of arrays. By using arrays.shift(), we can sequentially compare the first array to the remaining arrays until all arrays have been processed.

The above is the detailed content of How to Find Matching Elements Across Multiple JavaScript Arrays?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template