Skip to content

Instantly share code, notes, and snippets.

@asciimo
Created October 18, 2015 21:18
Show Gist options
  • Save asciimo/8aecf5ac6a06568d8b54 to your computer and use it in GitHub Desktop.
Save asciimo/8aecf5ac6a06568d8b54 to your computer and use it in GitHub Desktop.
Here's an example of how to sum the numeric values in a Wikipedia HTML table column.
// Written for this Wikipedia page on 2015-10-18:
// https://en.wikipedia.org/wiki/List_of_motor_vehicle_deaths_in_U.S._by_year
var deaths = 0;
$("table.wikitable").first().find("tr").each(
function() {
var deathsCell = $(this).find("td").first().text();
var pattern = new RegExp("([\d,,]+)[^\d,,]?")
deathsNum = parseInt(deathsCell.replace(/,/g, ""), 10);
if(!isNaN(deathsNum)) {
deaths += deathsNum;
}
}
);
console.log(deaths);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment