jquery - Create list / array of table values -
i have table 6 fixed columns looks below , variable content created dynamically. within each column value can appear once may not appear in columns.
is there way can list / array values columns cat , volumes columns vol example variables below?
my table:
<table id="mytable"> <thead> <tr> <th class="myheader">cat 1</th> <th>vol 1</th> <th class="myheader">cat 2</th> <th>vol 2</th> <th class="myheader">cat 3</th> <th>vol 3</th> //... </tr> </thead> <tbody> <tr> <td>item1</td><td>8</td><td>item2</td><td>7</td><td>item3</td><td>9</td> </tr> <tr> <td>item3</td><td>5</td><td>item2</td><td>7</td><td>item1</td><td>4</td> </tr> <tr> <td>item2</td><td>1</td><td>item1</td><td>5</td><td>item3</td><td>3</td> </tr> //... </tbody> </table>
required output:
var item1 = [8, 4, 5] var item2 = [7, 7, 1] var item3 = [9, 5, 3]
try this:
$(document).ready(function() { var items ={ item1: [], item2: [], item3: [] }; $('#mytable > tbody > tr').each(function() { var cols = $(this).find('td'); (var col = 0; col < cols.length; col += 2) { items[$(cols[col]).text()].push(+$(cols[col + 1]).text()); } }); console.log(items); });
working example: http://jsfiddle.net/qlykk/ (i left item empty show in case 0 put in array)
Comments
Post a Comment