+ 2

Whay the callback function takes to parameters to remove duplicates array and explain what does "names.indexOf(v)===i" do?

let names = ['John', 'Paul', 'George', 'Ringo', 'John']; let filtering = names.filter((v,i) => names.indexOf(v) === i ) Console.log(filtering)

6th Apr 2020, 3:42 PM
Arsham Aazami
Arsham Aazami - avatar
2 Respuestas
+ 2
callback used in almost array methods wich have one receive up to 3 arguments: value, index, array... so you could also write: let filtering = names.filter( (v,i,a) => a.indexOf(v)==i ); One exception is the reduce method, wich take first a fourth argument (the accumulator)...
6th Apr 2020, 3:53 PM
visph
visph - avatar
+ 1
Thank you my bro
6th Apr 2020, 3:58 PM
Arsham Aazami
Arsham Aazami - avatar