4 ms·
Wouldn't do it with variable, since it will make your function impure [1] 1 : http://www.nicoespeon.com/en/2015/01/pure-functions-javascript/ http://www.nicoes
by drinchev 9y ago
Wouldn't do it with variable, since it will make your function impure [1]
1 : http://www.nicoespeon.com/en/2015/01/pure-functions-javascript/ http://www.nicoespeon.com/en/2015/01/pure-functions-javascri...
- pdpi 9y agoThat's the GP's point though: it already is impure! If you rewrite as let acc = {...} footballPlayers.reduce(...,acc) It should become clear: acc is an external variable that got mutated by the reduce.
- bestest 9y agoIndeed. A pure function should not depend on and / or mutate external variables. I'd do it like this (yay for one-ish-liners!): const playersByCountry = footballPlayers.reduce((acc, player) => { return {...acc, [player.country]: [...(acc[player.country] || []), player]}; }, {});