Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSeokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsFor a better understanding for the second code in the teacher's note
const numbers = [1, 1, 2, 3, 4, 3, 5, 5, 6, 7, 3, 8, 9, 10];
const unique = numbers.filter(function(number, index, array) {
return index === array.indexOf(number);
});
console.log(unique); // => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
/** write process one by one
* (num, index) => return index === array.indexOf(number);
* (1, 0) => return 0 === array.indexOf(1); true (new index)
* (1, 1) => return 1 === array.indexOf(1); false (exist already)
* (2, 2) => return 2 === array.indexOf(2); true (new index)
* (3, 3) => return 3 === array.indexOf(3); true (new index)
* (4, 4) => return 4 === array.indexOf(4); true (new index)
* (3, 5) => return 5 === array.indexOf(3); false (exist already)
* (5, 6) => return 6 === array.indexOf(5); true (new index)
* (5, 7) => return 7 === array.indexOf(5); false (exist already)
* (6, 8) => return 8 === array.indexOf(6); true (new index)
* (7, 9) => return 9 === array.indexOf(7); true (new index)
* (3, 10) => return 10 === array.indexOf(3); false (exist already)
* (8, 11) => return 11 === array.indexOf(8); true (new index)
* (9, 12) ==> return 12 === array.indexOf(9); true (new index)
* (10, 13) => return 13 === array.indexOf(10); true (new index)
*/
// returns [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
According to my analysis, the whole process will be like above as I commented, and indices of the array "unique" are not technically correct, but they just stack up one by one. (as you can see array.indexOf(2) === 2, but its index in the output array "unique" is actually "1" ).
I understand the fact that the "filter()" segregates elements depending on "true" or "false" condition, but please can somebody explain me if I am right, or is there any logical error in my explanation. Thank you so much for you time in advance.
2 Answers
Steven Parker
231,248 PointsThe indices of the array "unique" are not significant, that array just contains a filtered list of values in the order in which they were first found.
You're right that "filter" returns only the items for which the callback function returned "true".
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsI got it. So actual indices are more like just for ordered stacks. Thanks for making it a bit more clear.
Jonathan Drake
Full Stack JavaScript Techdegree Student 11,668 PointsThis was very helpful. Thanks for taking the time to write that out.
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsSeokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 Pointsconst easyWay = Array.from(new Set(numbers));
just for another better understanding, this code works the same as "unique" :)