const types = ['in', 'out'];
const years = [2000, 2005, 2010, 2015, 2016, 2017];
const regions = ['Africa', 'Americas', 'Asia', 'Europe', 'Oceania'];
var dataset = {};
for (let typ of types) {
dataset[typ] = {};
for (let year of years) {
dataset[typ][year] = {};
for (let region of regions) {
dataset[typ][year][region] = [];
}
}
}
/*
Convert regions to an object with each region as a property and
the region's value as an empty array.
*/
const regionsObj = regions.reduce((acc, region) => {
acc[region] = []
return acc
}, {}) // The initial value of the accumulator (`acc`) is set to `{}`.
function copyObj(obj) {
return JSON.parse(JSON.stringify(obj))
}
/*
Do the same thing with the years, but set the value
for each year to the regions object.
*/
const yearsObj = years.reduce((acc, year) => {
acc[year] = copyObj(regionsObj)
return acc
}, {})
// One more time for the type. This will return our final object.
const dataset = types.reduce((acc, type) => {
acc[type] = copyObj(yearsObj)
return acc
}, {})