When given two arrays of integers, I aim to create a new array containing only the unique values from both input arrays. There are many methods for achieving this, but I'm curious as to why my current approach is not producing the desired result.
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [2, 3, 1, 0, 5];
// combining both arrays
const combined = arr1.concat(arr2);
// sorting the combined array
combined.sort();
// filtering out elements that are the same as the preceding element
const result = combined.filter((el, i, a) => {
if ( (el != a[i-1]) ) {
console.log("return", el);
return el;
}
});
// printing the output
console.log(result);
Despite zero being present in the combined array, the final output displays [1,2,3,4,5]. Upon using console.log, it's evident that the filter method is returning the element zero, yet it doesn't appear in the result array. What could be the issue?