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 trial

JavaScript JavaScript Array Iteration Methods Combining Array Methods Chaining Array Methods

Seokhyun Wie
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 Points

For 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
Steven Parker
231,236 Points

The 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".