This is the fake data that I've made for testing:
const data =[
{
country: 'US',
city: 'NY',
population: 1000
},
{
country: 'CA',
city: 'TOR',
population: 1000
},
{
country: 'US',
city: 'LA',
population: 1000
},
{
country: 'CH',
city: 'BEI',
population: 1000
},
{
country: 'CA',
city: 'VAN',
population: 1000
},
{
country: 'CH',
city: 'SHANG',
population: 1000
},
]
Let's just pretend that we're working with an API with 100+ countries and many more cities. How do I make a new array out of this array that looks like this:
const finalData = [
{
country: 'US',
population: 2000
},
{
country: 'CA',
population: 2000
},
{
country: 'CH',
population: 2000
},
]
So basically grouping them based on their countries and adding the population from cities with the same country.