Created
October 18, 2015 21:18
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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