I'm beginning working with reduce and I'm having a hard time conceptualizing how to use it. I understand it when using numbers, but when it comes to objects and other data, I have a hard time following the logic. I want to take an array of objects and return an object with the keys of countryName and the value being an object with the rest of the country data. Any help would be greatly appreciated!
Data
var countries = [
{
"countryCode": "AF",
"countryName": "Afghanistan",
"population": "29121286",
"capital": "Kabul",
"continentName": "Asia"
},
{
"countryCode": "AL",
"countryName": "Albania",
"population": "2986952",
"capital": "Tirana",
"continentName": "Europe"
},
{
"countryCode": "DZ",
"countryName": "Algeria",
"population": "34586184",
"capital": "Algiers",
"continentName": "Africa"
},
]
Expected Output
{
Afghanistan: {
"countryCode": "AF",
"population": "29121286",
"capital": "Kabul",
"continentName": "Asia"
},
Albania: {
"countryCode": "AL",
"population": "2986952",
"capital": "Tirana",
"continentName": "Europe"
},
Algeria: {
"countryCode": "DZ",
"population": "34586184",
"capital": "Algiers",
"continentName": "Africa"
},
}
Base Attempt
function organizeByCountry(countries) {
return countries.reduce((acc, country) => {
return country.countryName
}, {})
}