[Solved] Why can’t I sort AND deduplicate my Array?


Shouldn’t ‘if’ allow me to remove duplicates?

No, sorting will not remove elements ever. It just changes the order.

Calling map(…) or forEach(…) will do the same: 1 to 1 in respect to the array’s elements.

Only filter(…) and reduce(…) will iterate and modify the number of items in the returned array.

BTW here’s your filter function:

function unique (value, index, self) { 
    return self.indexOf(value) === index;
}

myArray.sort(sortFunction).filter(unique)

2

solved Why can’t I sort AND deduplicate my Array?