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()
}));
(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>
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With