I'm solving a problem where a function array_diff
returns values in arraya
that are also in array b
.
Since I've been learning about named function expressions being better for console debugging than anonymous fat arrow functions, I'm trying to solve this problem with named removeDuplicate
function to filter my array.
However, I am not able to prevent the filter function from automatically removing the falsey value 0 from my returned array.
Named function expression:
function array_diff(a, b) {
return a.filter(function removeDuplicate(x) { if(b.indexOf(x) == -1) return x; });
}
array_diff([0,1,2,3,4],[2,4]); // [1, 3]
Anonymous Fat Arrow function:
function array_diffTwo(a, b) {
return a.filter((x) => { return b.indexOf(x) == -1 });
}
array_diffTwo([0,1,2,3,4],[2,4]); // [0, 1, 3]
Can someone explain to me why the falsey value 0 is removed in array_diff
and not array_diffTwo
?