1

I am working on this demo. How can I export each row of the table in an array inside of an array?

I need the output like like

arrayofArray = [
                 [49, 16, 135,  32, 53],
                 [51, 16, 140,  34, 55],
                 [66, 12' 140,  36, 50]
]

var length =  $('table tbody tr').length;   
var arrayofArrays =[];
for (var i = 0; i < length; i++) { 
   arrayofArrays.push(parseInt($('table tbody tr:eq('+i+') td').text().trim()))
} 

console.log(arrayofArrays);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
	<tbody>
		 <tr>
				<td align="center"> 49 </td>
				<td align="center"> 16 </td>
				<td align="center"> 135 </td>
				<td align="center"> 32 </td>
				<td align="center"> 53 </td>
		 </tr>
		 <tr>
				<td align="center"> 51 </td>
				<td align="center"> 16 </td>
				<td align="center"> 140 </td>
				<td align="center"> 34 </td>
				<td align="center"> 55 </td>
		 </tr>
		 <tr>
				<td align="center"> 66 </td>
				<td align="center"> 12 </td>
				<td align="center"> 140 </td>
				<td align="center"> 36 </td>
				<td align="center"> 50 </td>
		 </tr>
	</tbody>
</table>

2
  • Looks like you are just missing the loop over all the TD's in the row. You are just grabbing the first TD Commented Aug 27, 2018 at 20:31
  • @MonaCoder As Adam said, you need a an inner for loop to iterate the elements of the array contained in the array of arrays Commented Aug 27, 2018 at 20:32

2 Answers 2

2
let arrayOfArrays = [];

//You can iterate each table row of the table body like so
$('table tbody tr').each(function(){
    //Declare a new array object to hold all cell values for this row
    let newArray = [];
    //Iterate each table cell of the row, currently this refers to the table row
    $(this).find('td').each(function(){
         newArray.push(parseInt($(this).text()));
    });
    //Push row array into main array
    arrayOfArrays.push(newArray);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Hi Ryan thanks for reply but my question is loading array from a table not table to array :-)
@MonaCoder I'll change my answer, one minute.
@MonaCoder Updated the answer for you, sorry, I misunderstood what you were trying to do originally.
0

Here is a VanillaJs version.

let table = document.querySelector("table"),
    tBody = table.tBodies[0];

console.log(
  Array.from(tBody.rows)
    .map(row => Array.from(row.cells)
      .map(cell => cell.innerText * 1))
);
<table>
  <tbody>
    <tr>
      <td align="center"> 49 </td>
      <td align="center"> 16 </td>
      <td align="center"> 135 </td>
      <td align="center"> 32 </td>
      <td align="center"> 53 </td>
    </tr>
    <tr>
      <td align="center"> 51 </td>
      <td align="center"> 16 </td>
      <td align="center"> 140 </td>
      <td align="center"> 34 </td>
      <td align="center"> 55 </td>
    </tr>
    <tr>
      <td align="center"> 66 </td>
      <td align="center"> 12 </td>
      <td align="center"> 140 </td>
      <td align="center"> 36 </td>
      <td align="center"> 50 </td>
    </tr>
  </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.