Last active
August 29, 2015 14:07
-
-
Save AmrEldib/0ce79b812c1854bb37fb to your computer and use it in GitHub Desktop.
Convert HTML Table to JSON Array
This file contains 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
// Make sure to rename the object fields | |
// Table's header gets ignored | |
var tbl = $('table#tableToConvert tr:has(td)').map(function(i, v) { | |
var $td = $('td', this) | |
return { | |
wkid: $td.eq(0).text(), | |
name: $td.eq(1).text() | |
} | |
}).get(); | |
$('#result').html(JSON.stringify(tbl)); | |
// Sample HTML snippet | |
// <table id="tableToConvert" cellspacing="0"> | |
// <thead> | |
// <tr> | |
// <th class="tableheader">Well-known ID</th> | |
// <th class="tableheader">Name</th> | |
// </tr> | |
// </thead> | |
// <tbody> | |
// <tr> | |
// <td> 103971</td> | |
// <td>NAD_1983_HARN_Adj_WI_Wood_Feet</td> | |
// </tr> | |
// </tbody> | |
// </table> | |
// <p id="result"></p> | |
// Source: http://stackoverflow.com/a/12291010/463 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment