У меня есть следующие массивы:
const countries = ['Belgium', 'Uk']
const years = ['2019', '2018', '2017']
const colors = ['red', 'orange', 'green']
Мне нужен такой массив:
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,
},
]
Итак, для каждого года и для каждой страны должен существовать объектсодержащий ключи каждого цвета.Значение должно быть случайным.
Тогда должны быть другие объекты с country = Total
и со значениями цветов, набирающими сумму значений других объектов.
Это то, что я пытаюсьделать:
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),})})})})}
Но это не работает, и я не знаю, как вычислить значения суммы.