3

I have an ajax function whose response is a html table.I need to convert it into array.

My response is like the following:

<table>
    <tr>
        <td>4362</td>
        <td>Education</td>
    </tr>
    <tr>
        <td>4373</td>
        <td>Education world</td>
    </tr>
</table>

I need to change this to following array.

[['4362','Education'],['4373','Education world']]

I have done the following ajax code but is not giving me the exact form.

success:function(hasil) { 
   var table_response = $.parseHTML($(hasil).filter('table').html());
   console.log($(table_response).each(function(index, tr) {
          $('td', tr).map(function(index, td) {
               return $(td).text()
          })
   }));
}

I have tried another code also,but its giving me all td contents in the array

console.log($('td', table_response).map(function(_, el) {  
              return $(el).html()  
}));
Waleed Iqbal
  • 1,357
  • 16
  • 28
Techy
  • 2,288
  • 6
  • 35
  • 77
  • This may help you https://stackoverflow.com/questions/34349403/convert-table-to-array-in-javascript-without-using-jquery – pitaridis Jan 16 '18 at 06:58
  • Possible duplicate of [How do I iterate through table rows and cells in JavaScript?](https://stackoverflow.com/questions/3065342/how-do-i-iterate-through-table-rows-and-cells-in-javascript) – Satendra Jan 16 '18 at 06:59

4 Answers4

1

try this https://jsfiddle.net/492f9y84/1/

   var arr=[]
   var table_response = $.parseHTML($(hasil).filter('table').html());
   $(table_response).find("tr").each(function(index, tr) {
        var trArr = []
      $(tr).find('td').each(function(index, td) {
           trArr.push($(td).text())
      })
      arr.push(trArr)
   });

   console.log(arr)
Aswin Ramesh
  • 1,554
  • 1
  • 11
  • 13
1

(function (){

  var response = `<table><tr><td>4362</td><td>Education</td></tr>
         <tr><td>4373</td><td>Education world</td>
         </tr>
  </table>`;

    var arr = [];

    $(response).find('tr').each(function(index){
        var row = arr[index] = [];
        $(this).find('td').each(function(i){
            row[i] = $(this).text();
        })
    })
    
    console.log(arr);

})()
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Shridhar Sharma
  • 2,185
  • 1
  • 6
  • 12
1

var result = []; 
$("#tbl").find("tr").each(function(index, tr) {
      var temp = [];
      $(tr).find("td").each(function(index, td) {
          temp.push($(td).text());
      });
      result.push(temp);
});

console.log(result);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tbl"><tr><td>4362</td><td>Education</td></tr>
       <tr><td>4373</td><td>Education world</td>
       </tr>
</table>
Lalit
  • 1,284
  • 1
  • 7
  • 13
0
function storearrValues()
{
    var TableData = new Array();

    $('#myDemotable tr').each(function(row, tr){
        TableData[row]={
            "Id" : $(tr).find('td:eq(0)').text()
            , "Value" :$(tr).find('td:eq(1)').text()
        }    
    }); 
    TableData.shift();  // first row will be empty - so remove
    return TableData;
}
var getData=JSON.stingify(storearrValues())
Sagar Shinde
  • 142
  • 9