records = VARIANTS.reduce((acc, variant) => {
const records = regions.map(({ key: regionKey }) => {
const selected = rows.filter(row => row.regionKey === regionKey && row.variant === variant);
const regionName = selected[0].region;
const structure = secondaryKeys.map(year => {
const row = selected.find(row => row.year === year);
const total = row.pop.reduce((acc, value) => acc + value, 0);
return {
year,
amounts: row.pop,
fractions: row.pop.map(value => (value / total) || 0),
total,
dependencyRatio: popToDependencyRatio(row.pop)
}
});
return { variant, regionName: regionName, regionKey: regionKey, structure };
});
return [...acc, ...records];
}, []);