javascript-将html响应放入数组

我有一个ajax函数,其响应是一个html表.我需要将其转换为数组.

我的回答如下:

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

我需要将其更改为以下数组.

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

我已经完成了以下ajax代码,但没有给我确切的形式.

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()
          })
   }));
}

我也尝试了另一个代码,但是它给了我数组中所有的td内容

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>

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...