Intermediate Algorithm: Sorted Union -- filter function not working

Ok. I don’t know why this isn’t working. Can someone enlighten me?

function uniteUnique(arr) {
  arr = Array.prototype.slice.call(arguments);
  arr = [].concat.apply(arr);
  arr = arr.filter( function (item, pos) {
    return arr.indexOf(item) == pos;
  });

  return arr;
}

uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);

I was expecting the first line to create the array that you wrote.

I am expecting the second line to flatten out the array (remove the nested array).

I am expecting the filter function to remove any duplicates but leave the first occurrence.

[ 1, 3, 2,5, 2, 1, 4 , 2, 1 ]

Oh! It should be the following:

arr = [].concat.apply([], arr);

Right?

Yep. You can break it down conceptually like this:

['placeholder'].concat.apply(['original', 'array'], [['arrays'], ['to'], ['concatenate']]);
// => ["original", "array", "arrays", "to", "concatenate"]

Note that the placeholder array is discarded (regardless of whether or not it contains any data) — the only reason we need it is as shorthand to get that concat method from the array prototype:

Array.prototype.concat === [].concat;
// => true

Wow! thanks to both of you. Very helpful.