Sorted Union I have a question!

Tell us what’s happening:
I just finished the test, but when I consulted other ways, I encountered a code using the filter method. And after reading the code I have a question like this:

Your code so far

function uniteUnique() {
  var concatArr = [];
  var i = 0;
  while (arguments[i]){
    concatArr = concatArr.concat(arguments[i]); i++;
  uniqueArray = concatArr.filter(function(item, pos) {
    return concatArr.indexOf(item) == pos;
  return uniqueArray;

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

What role does the ‘pos’ parameter have in the filter function and what is its value and how does it work?

Link to the challenge:

pos is the index of the item in the array
It is checking if the first appearance of a certain item is at that position or not

1 Like

i got it, thank you !!!

the second argument in a filter callback function is the index of element in the array being filtered

[a,b,c,d].filter(function(item, idx) {
// a 0
// b 1
// c 2
// d 3

so pos above is the index, the method indexOf() applied in an array returns the first occurrence of the item being searched for, combining the index above with the first occurrence in the filter eliminates any chance for duplicates.

1 Like

A very detailed and understandable answer, thanks.

glad it helped, I had problems wrapping my head around it too at first, but the key is that indexOf() always returns the first occurrence…

1 Like