Skip to content

Instantly share code, notes, and snippets.

@pmcelhaney
Created June 5, 2012 15:01
Show Gist options
  • Save pmcelhaney/2875523 to your computer and use it in GitHub Desktop.
Save pmcelhaney/2875523 to your computer and use it in GitHub Desktop.
map() is your friend
/*
map() is cool!
This function essentially loops over each row of a table and puts
its data in an array of objects.
It returns something like this:
[
{rate: 1.0, apy: 1.0, productLabel: '3 month CD', earnings: 123.45},
{rate: 1.12, apy: 1.13, productLabel: '6 month CD', earnings: 543.21},
...
]
*/
var rateData = myTable.find('tbody > tr').map(function () {
var tr = $(this);
return {
rate: extractNumber(tr.children('.rate').text()),
apy: extractNumber(tr.children('.apy').text()),
productLabel: tr.children('.term').text(),
earnings: extractNumber(tr.children('.earnings').text())
};
});
var extractNumber = function (str) {
return parseFloat(str.replace(/[^0-9.]/g, ""));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment