I have two arrays
a. [1,2,3,4,5]
b. [2,3,4,5,6]
I try to find 2,3,4,5 with array.reduce
because I think it is more efficient.
Can I do so?
I have two arrays
a. [1,2,3,4,5]
b. [2,3,4,5,6]
I try to find 2,3,4,5 with array.reduce
because I think it is more efficient.
Can I do so?
This will get you the same result without using reduce
:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
result = a.filter(p=>b.includes(p));
console.log(result);
Or with reduce:
var a=[1,2,3,4,5];
var b= [2,3,4,5,6];
var result = b.reduce((acc,elem)=>{
if(a.includes(elem)) acc.push(elem);
return acc;
},[]);
console.log(result);