Published
Edited
Mar 16, 2022
Insert cell
Insert cell
Insert cell
Insert cell
analysesRaw = (await fetch(`${endpoint}analyses?type=A&key=${key}`)).json();
Insert cell
Insert cell
countries = analysesRaw.reduce((memo, val) => memo.includes(val.country) ? memo : [...memo, val.country], []);
Insert cell
Insert cell
population = await Promise.all(
countries.map((country) => {
return fetch(
`${endpoint}population/?start=2019&end=2022&country=${country}&key=${key}`
).then((res) => res.json());
})
)
Insert cell
Insert cell
function testDateRange(range) {
if (!range) return false;
const dates = range.split(' - ').map(parseDate);
if (dates.length !== 2) return false;
const now = new Date();
return now >= dates[0] && now <= dates[1]
}
Insert cell
results = population
.map(analyses => {
const [latest] = Object.values(analyses).sort((a, b) => parseDate(b.analysis_date) - parseDate(a.analysis_date));
if (latest) {
let period = 'C';
let { population, population_percentage, p3plus, p3plus_percentage, country } = latest;
if(testDateRange(latest.second_projected_period_dates)) {
period = 'A';
p3plus = latest.p3plus_second_projected;
p3plus_percentage = latest.p3plus_percentage_second_projected;
}
if(testDateRange(latest.projected_period_dates)) {
period = 'P';
p3plus = latest.p3plus_projected;
p3plus_percentage = latest.p3plus_percentage_projected;
}
return {
country,
period,
population,
population_percentage,
p3plus,
p3plus_percentage,
}
}
});
Insert cell
table(results)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
parseDate = d3.timeParse('%b %G');
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more