Skip to content

Instantly share code, notes, and snippets.

@joeSaad
Created May 12, 2015 16:01
Show Gist options
  • Save joeSaad/1599dab00dab70604ac9 to your computer and use it in GitHub Desktop.
Save joeSaad/1599dab00dab70604ac9 to your computer and use it in GitHub Desktop.
Putting a table into an array of objects
/*
table>thead(>tr>th{column $}*4)+tbody(>tr*5>td{data $}*4)
*/
<table>
<thead>
<tr>
<th>column 1</th>
<th>column 2</th>
<th>column 3</th>
<th>column 4</th>
</tr>
</thead>
<tbody>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
<td>data 4</td>
</tr>
</tbody>
</table>
var tArr = [];
var tObj = {};
for (var i=0; i< $('tbody tr').length; i++){
for (var j = 0; j< $('th').length; j++){
tObj[$('th').eq(j).text()] = $('tbody tr:eq('+i+') td:eq('+j+')').text();
//console.log(tObj);
}
tArr.push(tObj);
}
console.log(tArr);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment