January 2020
Intermediate to advanced
470 pages
11h 13m
English
There is a simple function that we could also produce. Extracting an attribute from an object is a commonly required operation. For example, in Chapter 5, Programming Declaratively – A Better Style, we had to get latitudes and longitudes to be able to calculate an average. The code for this was as follows:
markers = [ {name: "UY", lat: -34.9, lon: -56.2}, {name: "AR", lat: -34.6, lon: -58.4}, {name: "BR", lat: -15.8, lon: -47.9}, ... {name: "BO", lat: -16.5, lon: -68.1}];let averageLat = average(markers.map(x => x.lat));let averageLon = average(markers.map(x => x.lon));
We saw another example of this when we learned how to filter an array; in our example, we wanted to get the IDs for all the accounts with ...