Build an array of objects from some arrays

  • A+
Category:Languages

I have these 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.

Any help is appreciated, thanks a lot!

 


My solution

const countries = ['Belgium', 'Uk']; const years = ['2019', '2018', '2017']; const colors = ['red', 'orange', 'green'];  const min = 0; const max = 300;  console.log(getTotal(   getResult(countries, years, colors),   colors   ));  function getResult(countries, years, colors) {   const result = [];   countries.forEach(country => {     years.forEach(year => {       const item = {         country: country,         year: year       };       colors.forEach(color => {         item[color] = random(min, max);       });       result.push(item);     });   });   return result; }  function getTotal(data, colors) {   const total = {};   data.forEach(item => {     if (typeof total[item.year] === 'undefined') {       total[item.year] = {};       colors.forEach(color => {         total[item.year][color] = item[color];       });     } else {       colors.forEach(color => {         total[item.year][color] += item[color];       });     }   });   for (var totalItem in total) {     data.push({country: 'Tot', year: totalItem, ...total[totalItem]});   }   return data; }  function random(min, max) {     return Math.floor(Math.random() * (max - min)) + min; }

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: