How to get the common values of an indefinite number of arrays in Javascript

Marco Pestrin
1 min readDec 24, 2020

Some days ago I needed to write an algorithm in javascript to get the common values of an indefinite number of arrays.

Let’s look at the following case:

Desired result:

In this case the common values are ivr, add and web meanwhile the others were excluded because they were not present in all arrays.

Seen my case, I’m not aware how many element an array can have and also how many arrays can I have. But the once thing that I aware was the possible data within the array.

I fully leaned on this last and only piece of information I had available to avoid too many cycles.

The solution that I used to get this algorithm is:

I have created an array with all possible values (commonChannels).

I started to cycle all arrays then every within item each

Whenever an element was missing it was excluded from the array. To do this I rewrote the base variable (commonChannels)

To exclude the elements I’ve combined of the Filter and IndexOf methods.

--

--