I've understood how to use filter
, but seeing this answer to this exercise: Eloquent JavaScript confuses me (you can see the code when you click 'look at the solution'. Just what is the difference between:
let counted = countBy(text, char => {
let script = characterScript(char.codePointAt(0));
return script ? script.direction : "none";
}).filter(({name}) => name != "none");
and:
let counted = countBy(text, char => {
let script = characterScript(char.codePointAt(0));
return script ? script.direction : "none";
}).filter(name => name != "none"); //The difference is here and when I tried this, it did not work
What I'm familiar with is using ... .filter(something => condition);
and it will return an array of all the items from the original array that fulfills said condition. I tried changing the above code to ... .filter(script => script.name != "none")
and it worked. So what exactly does ({name})
do so specially that I don't have to type that long?