-3

So I want to compare 2 arrays with each other to count the similarities that they have.

[ 401, 271, 259, 260, 342, 410 ]

[ 401, 12021, 923, 235, 342, 410 ]

The above arrays should output 3. Right now I have a forEach inside a forEach just going through each one individually to check the other array individually for the same number.

WeLoki1
  • 1
  • 4

1 Answers1

0

you can use the Array.reduce() method

const a = [ 401, 271, 259, 260, 342, 410 ]

const b = [ 401, 12021, 923, 235, 342, 410 ]


console.log(a.reduce((acc,curr, i)=>{
    if(curr==b[i]) acc+=1
    return acc
},0));
MWO
  • 2,627
  • 2
  • 10
  • 25