Public
Edited
Dec 2, 2022
1 fork
1 star
Insert cell
Insert cell
api_parameters = {
let obj = {};
obj.page = "List_of_countries_by_population_(United_Nations)";
obj.format = "json";
obj.origin = "*";
obj.action = "parse";
obj.section = "0";
obj.prop = "text";
return obj;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rows = d3
.select(wiki_table)
// .selectAll("table")
// .filter((d, i) => i === 1)
.node()
.querySelectorAll("tr:not(:first-child)")
Insert cell
d3.selectAll(rows).size();
Insert cell
countries = {
let flags = [];
d3.select(wiki_table).selectAll("img").each(function () {
flags.push("https:" + d3.select(this).attr("src"));
});
let ar = [];
d3.selectAll(rows).filter((d,i) => i < 233).each(function (d, index) {
const record = d3.select(this).text().trim().split("\n");
const element = {};
record.forEach((text, i) => {
switch (i) {
case 0:
// element.country = text.replace(/\[.*\]/, "");
const country = text.replace(/\[.*\]/, "");
const territory = country.substring(country.indexOf("(") + 1, country.length - 1);
if (record[0].indexOf('(') === -1) {
element.country = country;
element.territory = country;
} else {
element.country = country.substring(0, country.indexOf(" ("));
element.territory = territory;
}
break;
case 1:
element.region = text;
break;
case 2:
element.subregion = text;
break;
case 3:
element.population_2018 = +text.replaceAll(",","");
break;
case 4:
element.population_2019 = +text.replaceAll(",",""); //parseNumber(text);
break;
case 5:
element.change = convert(text);
break;
default:
break;
}
});
element.flag = flags[index];
ar.push(element);
});
return ar;
}
Insert cell
function convert(str) {
return +(str.replace("%","").replace("−","-").replace(".","")) / 100.0;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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