DEV Community

Discussion on: Using fetch() and reduce() to grab and format data from an external API - A practical guide

Collapse
 
spunkie profile image
Spunkie

I'm sure there are some super awesome things you can do with reduce() but the examples given just seem to provide the same functionality as a foreach loop?

$cityJobsData = json_decode(SOME_JSON, true);
$agencyFrequency = array();

foreach($cityJobsData as $city) {
    $agency = $city['agency'];

    if(!isset($agencyFrequency[$agency])) {
        $agencyFrequency[$agency] = 0;
    }
    $agencyFrequency[$agency]++;
}
Enter fullscreen mode Exit fullscreen mode

Is there a reduce() example you could give that could not be easily accomplished with a foreach or ends up being significantly more readable than the equivalent foreach loop? I'm primarily a php developer that is just getting into doing more serious javascript past basic jquery/dom stuff, so excuse me if I'm missing something.

Collapse
 
hulloanson profile image
hulloanson

It handles the object initialization for you, for one. Less typing, less bugs :)