I have the following arrays:
const countries = ['Belgium', 'Uk']
const years = ['2019', '2018', '2017']
const colors = ['red', 'orange', 'green']
I want an array like that:
const result = [
{
country: 'Belgium',
year: '2019',
red: random(min, max),
orange: random(min, max),
green: random(min, max),
},
{
country: 'Belgium',
year: '2018',
red: random(min, max),
orange: random(min, max),
green: random(min, max),
},
{
country: 'Belgium',
year: '2017',
red: random(min, max),
orange: random(min, max),
green: random(min, max),
},
{
country: 'Uk',
year: '2019',
red: random(min, max),
orange: random(min, max),
green: random(min, max),
},
{
country: 'Uk',
year: '2018',
red: random(min, max),
orange: random(min, max),
green: random(min, max),
},
{
country: 'Uk',
year: '2017',
red: random(min, max),
orange: random(min, max),
green: random(min, max),
},
{
country: 'Tot',
year: '2019',
red: // sum of the values of the red key for each country in the year 2019,
orange: // sum of the values of the orange key for each country in the year 2019,
green: // sum of the values of the green key for each country in the year 2019,
},
{
country: 'Tot',
year: '2018',
red: // sum of the values of the red key for each country in the year 2018,
orange: // sum of the values of the orange key for each country in the year 2018,
green: // sum of the values of the green key for each country in the year 2018,
},
{
country: 'Tot',
year: '2017',
red: // sum of the values of the red key for each country in the year 2017,
orange: // sum of the values of the orange key for each country in the year 2017,
green: // sum of the values of the green key for each country in the year 2017,
},
]
So, for each year, and for each country, there must be an object containing the keys of each color. The value must be random.
Then, should be other objects with country = Total
and with values of colors key the sum of the values of other objects.
This is what I'm trying to do:
function createValues() {
const min = 0
const max = 300
const dataset: any = []
countries.forEach(country => {years.forEach(year => {colors.forEach(color => {dataset.push({country: country, year: year, [color]: random(min, max),})})})})}
But it doesn't work and I don't know how to compute the sum values.