0

I'm trying to remove duplicate values in an array. As I was searching I found that I could use .filter() for the solution. Following code seems to work, but I don't understand how return array.indexOf( value ) === index; part works. Can someone explain what that part mean? So far I understand that .filter() returns values that is true.

var items = [3,2,2,4,8,8,8,6,9];

var result = items.filter( function( value, index, array ) {

    return array.indexOf( value ) === index;

})

console.log( result );
kayak
  • 440
  • 3
  • 7
  • 19
  • 2
    It's only keeping the elements that are found in their same position. So if a value appears twice, the second instance will find the first instance index, not match, and be excluded – Taplar Apr 17 '20 at 14:38
  • Just look at every single step and combine them. What does `array.indexOf(value)` do? What does `array.indexOf(value) === index` do and what is the result? How does `.filter()` work? – Andreas Apr 17 '20 at 14:40

0 Answers0