Hi - I am following a Udemy course on d3js and am currently stuck on a non-d3 specific problem so I thought I’d come to my trusty JS brain-trust for help!
I’m no JS whizz but I know a decent amount. But - I am not understanding what this code does - specifcally the line with year["countries"]:
const formattedData = data.map(function(year) {
return year["countries"]
.filter(function(country) {
var dataExists = country.income && country.life_exp;
return dataExists;
})
.map(function(country) {
country.income = +country.income;
country.life_exp = +country.life_exp;
return country;
});
});
Can someone please help me understand - in laymans terms - what is happening there?
This is a link to the data >>> https://github.com/SabahatPK/gapminder-clone/tree/master/data
Thank you.
ILM
September 4, 2019, 6:33pm
2
year["countries"] is an array on which are applied the map and filter method
this is a way to make the methods more readable, a single line of code is broken down in multiple lines to make it more easy to read
1 Like
No - I know that year[“countries”] is an array…but based on the JSON (linked in original post), what exactly is in this array…? That’s where I am getting confused.
ILM
September 4, 2019, 7:19pm
4
an example of one of those:
"countries": [
{
"continent": "europe",
"country": "Yugoslavia",
"income": null,
"life_exp": null,
"population": 4687422
},
{
"continent": "asia",
"country": "United Korea (former)",
"income": null,
"life_exp": null,
"population": 13740000
},
{
"continent": "asia",
"country": "Tokelau",
"income": null,
"life_exp": null,
"population": 1009
},
...
]
1 Like
Sorry to be so thick…but I’m still confused.
year["countries"] implies that we are trying to access the value, "countries", of an object called year.
So where is the object year… in this JSON?
ILM
September 4, 2019, 7:34pm
6
year is the parameter of the map method’s callback
in the JSON there is an array, map is used on this array and each element of the array is called year in the callback
2 Likes