DEV Community

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

kais_blog profile image
Kai • Edited

Here's a much easier way:

const arrays = [
  ["ivr", "web", "wap", "add", "acd"],
  ["web", "add"],
  ["ivr", "wap", "add", "web"],
  ["ivr", "web", "add"],

const result = arrays.reduce((carry, array) =>
  carry.filter((element) => array.includes(element))

console.log(result); // ["web", "add"]
Enter fullscreen mode Exit fullscreen mode

This way, it doesn't matter how many arrays you have and what their possible values are. Also, please note, that your code is partly incorrect. You are missing " for your strings.

Besides, you can find more tips like this here:

pestrinmarco profile image
Marco Pestrin Author

Amazing! Thank you so much for your tips.. Right, your solution might be better. Now I corrected my code with " for my strings