-3

I have two objects:

obj1 [
 'Anime',
 'Movies/TV Series',
 'Coding',
 'Cooking',
 'Sport/Fitness',
 'Gaming'
]

and

obj 2 [
 'Music',
 'Movies/TV Series',
 'Coding',
 'Party/Night Clubs',
 'Gaming',
 'Traveling',
 'Roleplay'
]

I need to count the number of common elements in arrays. How can I do it in javascript?

Thanks for help in advance!

MegaMix_Craft
  • 2,199
  • 3
  • 10
  • 36
Anis
  • 3
  • 5

1 Answers1

1

Just use a filter with includes:

var a=[
  'Anime',
  'Movies/TV Series',
  'Coding',
  'Cooking',
  'Sport/Fitness',
  'Gaming'
],b=[
  'Music',
  'Movies/TV Series',
  'Coding',
  'Party/Night Clubs',
  'Gaming',
  'Traveling',
  'Roleplay'
];
console.log(a.filter(e=>b.includes(e)).length);
Chang Alex
  • 505
  • 3
  • 11