Convert html table to array in javascript
Here's one example of doing what you want.
var myTableArray = [];
$("table#cartGrid tr").each(function() {
var arrayOfThisRow = [];
var tableData = $(this).find('td');
if (tableData.length > 0) {
tableData.each(function() { arrayOfThisRow.push($(this).text()); });
myTableArray.push(arrayOfThisRow);
}
});
alert(myTableArray);
You could probably expand on this, say, using the text of the TH to instead create a key-value pair for each TD.
Since this implementation uses a multidimensional array, you can access a row and a td by doing something like this:
myTableArray[1][3] // Fourth td of the second tablerow
Edit: Here's a fiddle for your example: http://jsfiddle.net/PKB9j/1/
Convert HTML table to Array (JavaScript)
You can call
methods like Array#slice
, Array#map
, and Array#reduce
on the table.rows
and tr.cells
NodeLists to convert your table to a nested data structure. This method will support an arbitrary number of columns in your table.
Demo Snippet
var rows = [].slice.call($('table')[0].rows)
var keys = [].map.call(rows.shift().cells, function(e) { return e.textContent.replace(/\s/g, '')})
var result = rows.map(function(row) { return [].reduce.call(row.cells, function(o, e, i) { o[keys[i]] = e.textContent return o }, {})})
console.log(result)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><table border='1'> <tr> <th>Display Name</th> <th>Title</th> <th>Scope</th> <th>ID</th> <th>Description</th> </tr>
<tr> <td>XmlFormLibrary</td> <td>XML Form Libraries</td> <td>Web</td> <td>00bfea71-1e1d-4562-b56a-f05371bb0115</td> <td>Provides support for XML form libraries for a site.</td> </tr> <tr> <td>LinksList</td> <td>Links Lists</td> <td>Web</td> <td>00bfea71-2062-426c-90bf-714c59600103</td> <td>Provides support for links lists for a site.</td> </tr> <tr> <td>workflowProcessList</td> <td>WorkflowProcessList Feature</td> <td>Web</td> <td>00bfea71-2d77-4a75-9fca-76516689e21a</td> <td>This feature provides the ability to create a list to support running custom form actions. </td> </tr></table>
How do I convert data from html table into array of object using vanilla javascript
You can use for
to loop thru each tr
. Find the td
on each tr
, If there is only 1, store the text on currentCountry
variable. If more than one, push the object to the result variable.
var currentCountry = "";var result = [];
var tr = document.querySelectorAll('table tr');
for (var i = 0; i < tr.length; i++) { var td = tr[i].querySelectorAll('td');
if (td.length === 1) currentCountry = td[0].innerHTML; else if (td.length > 1) { for (var a = 0; a < td.length; a++) { result.push({country: currentCountry,city: td[a].innerHTML}); } }}
console.log(result);
<table> <tbody> <tr> <td colspan="3">Canada</td> </tr> <tr> <td>Toronto</td> <td>Montreal</td> <td>Vancouver</td> </tr> <tr> <td colspan="3">USA</td> </tr> <tr> <td>New York</td> <td>Chicago</td> <td>Boston</td> </tr> <tr> <td>Washington</td> <td>Detroit</td> <td>Los Angeles</td> </tr> </tbody></table>
Convert table to array in JavaScript without using jQuery
With qSA and Array.prototype.map
is pretty simple.
var tableInfo = Array.prototype.map.call(document.querySelectorAll('#tableId tr'), function(tr){
return Array.prototype.map.call(tr.querySelectorAll('td'), function(td){
return td.innerHTML;
});
});
Convert HTML table to array
In jquery:
var tdCollection = $("table").find("td");
var array = [];
$.each(tdCollection, function(key, el){
array.push($(el).text());
});
console.log(array);
fiddle: http://jsfiddle.net/qqdwct7h/
But it would be better to set an id attribute for the table, beacuse using $(table) isn`t best way to select a certain table.
How to convert value from HTML table to JSON array in javascript
This will do the job:
const res=[...document.querySelectorAll("#stockinboundedittable tr")].slice(1).map(tr=>
[...tr.querySelectorAll("input")].slice(0).map(inp=>[inp.id.replace(/.*inboundedit/,"").replace(/\d+$/,""),inp.value]))
.reduce((a,c)=>{
a[c[0][1]]=
Object.fromEntries(c.slice(1));
return a},{});
console.log(res[2]);
<table style="width:100%; border-collapse: collapse; text-align: center;" id="stockinboundedittable">
<tr>
<th style="display:none;">subcategory</th>
<th>Sl.No</th>
<!--<th>I.U.Code</th>-->
<th>Item Name</th>
<th> old stock</th>
<th> new stock</th>
<th> Total Stock</th>
<th> qrt</th>
<th>Edit</th>
</tr>
<tr>
<td style="display:none;">tmcsubctgy_2</td>
<td>
<input style=" width: 100%;" id="slno1editinbound1" value="1" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedititemname1" value="Fresh Goat Meat - Curry Cut" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditoldstock1" value="20" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditnewstock1" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedittotalstock1" value="30" readonly="">
</td>
<td>
<input style=" width: 100%;" id="inboundeditqty1">
</td>
<td>
<button style="background: #fdd110; width: 100%;" id="stockinboundeditditbut1">EDIT</button>
</td>
</tr>
<tr>
<td style="display:none;">tmcsubctgy_5</td>
<td>
<input style=" width: 100%;" id="slno1editinbound2" value="2" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedititemname2" value="Everest - Kasur Methi" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditoldstock2" value="0" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditnewstock2" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedittotalstock2" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="inboundeditqty2">
</td>
<td>
<button style="background: #fdd110; width: 100%;" id="stockinboundeditditbut2">EDIT</button>
</td>
</tr>
<tr>
<td style="display:none;">tmcsubctgy_13</td>
<td>
<input style=" width: 100%;" id="slno1editinbound3" value="3" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedititemname3" value="Fresh Coconut Milk 150 ml" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditoldstock3" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditnewstock3" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedittotalstock3" value="20" readonly="">
</td>
<td>
<input style=" width: 100%;" id="inboundeditqty3">
</td>
<td>
<button style="background: #fdd110; width: 100%;" id="stockinboundeditditbut3">EDIT</button>
</td>
</tr>
<tr>
<td style="display:none;">tmcsubctgy_5</td>
<td>
<input style=" width: 100%;" id="slno1editinbound4" value="4" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedititemname4" value="Sakthi - Chicken Masala" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditoldstock4" value="20" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditnewstock4" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedittotalstock4" value="30" readonly="">
</td>
<td>
<input style=" width: 100%;" id="inboundeditqty4">
</td>
<td>
<button style="background: #fdd110; width: 100%;" id="stockinboundeditditbut4">EDIT</button>
</td>
</tr>
<tr>
<td style="display:none;">tmcsubctgy_2</td>
<td>
<input style=" width: 100%;" id="slno1editinbound5" value="5" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedititemname5" value="Goat Bones - Regular Soup Pack" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditoldstock5" value="0" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundeditnewstock5" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="stockinboundedittotalstock5" value="10" readonly="">
</td>
<td>
<input style=" width: 100%;" id="inboundeditqty5">
</td>
<td>
<button style="background: #fdd110; width: 100%;" id="stockinboundeditditbut5">EDIT</button>
</td>
</tr>
</table>
</table>
Related Topics
Localstorage Access from Local File
How to Run HTML Files Directly from Github, Instead of Just Viewing Their Source
Has Facebook Sharer.PHP Changed to No Longer Accept Detailed Parameters
React Router with Optional Path Parameter
How to Display Pie Chart Data Values of Each Slice in Chart.Js
How Does Shallow Compare Work in React
How to Send Variables from One File to Another in JavaScript
Testing Whether a Value Is Odd or Even
Enable/Disable Submit Button If Checkbox Is Checked/Unchecked
Window.Open and Pass Parameters by Post Method
Shorthand Http:// as // for Script and Link Tags? Anyone See/Use This Before
How to Convert an Iso Date to the Date Format Yyyy-Mm-Dd
Can JavaScript Access Iframe Elements from the Parent Page
How to Use Jquery in Chrome Extension
JavaScript Syntax (0, Fn)(Args)
How to Sequentially Run Promises with Q in JavaScript
Should I Use Encodeuri or Encodeuricomponent for Encoding Urls