Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Please explain the unusedTokens() getter method

in 'Getters Methods Solution' from 'Object Oriented Javascript by Example' course, Ashley use .filter() in the method like this.

class Player {
     get unusedTokens () {
         return this.tokens.filter( token =>  !token.dropped )

I do not understand what is inside the .filter()? shouldn't be this a callback function? did she use an => arrow function? if so, where is the parameter?

2 Answers

Kallil Belmonte
Kallil Belmonte
35,559 Points

As the documentation states the .filter() receives a function inside of it. In this case she is using an arrow function where the first parameter is the currentValue (the token). So basically she is returning all tokens that are not "dropped": token => !token.dropped


.filter() : https://www.w3schools.com/jsref/jsref_filter.asp

Arrow function (just search for it on this page) : https://www.w3schools.com/js/js_es6.asp

thank you for explaining, helped me understand it much more, but I still don't get how is the parameter 'token' can iterate the data in the array 'this.tokens'

Kallil Belmonte
Kallil Belmonte
35,559 Points

Actually is not the parameter that iterates the array, what is iterating is the .filter() method. The paremeter is just the current value that is being iterated.

In the end, the filter will return another array, one with all the items that pass that condition.