I have some working code, though it seems like it should be much more efficient. I feel there are too many filters working through the same data sets. Is there a cleaner or more efficient way of merging 2 arrays without duplicating?
const x = [1,3,7,4,9];
const y = [2,3,9,13,4];
const yFilteredByX = y.filter(element => x.includes(element));
const xFilteredByY = x.filter(element => y.includes(element));
const unique = (value, index, self) => {
return self.indexOf(value) === index;
};
const newArr = yFilteredByX.concat(xFilteredByY);
const uniqueArr = newArr.filter(unique);
console.log(uniqueArr);
Currently outputs [3,9,4]
successfully.
I made a quick fiddle