简体   繁体   English

遍历表并将值存储为键/值对

[英]Travsering table and storing values as key/value pair

I have an html table like 我有一个html表

  <table>
    <tbody><tr><th>Course Name</th>
    <th>Course ID</th>
    <th>Value</th>
    </tr><tr>
<td>Course A</td>  
<td class="asset-id">1616781</td>  
<td class="asset-val">1306886</td>
</tr>

<tr>
<td>Course B</td>  
<td class="asset-id">723507</td>  
<td class="asset-val">1306886</td>
</tr>

<tr>
<td>Course C</td>  
<td class="asset-id">723503</td>  
<td class="asset-val">1306886</td>
</tr>

I want to traverse this table and create an Array of objects like 我想遍历此表并创建一个对象数组

[{
    "id": 1616781,
    "value": 1306886
}, {
    "id": 723507,
    "value": 1306886
}, {
    "id": 723503,
    "value": 1306886
}]

I have tried something like this so far, but its not working ... 到目前为止,我已经尝试过类似的方法,但是它不起作用...

 var myArray = [];
 $('.asset-id, .asset-val').each(function(i, obj) {                
            var tmpObj = {};
            var asset_key='';
            var asset_text='';

            tmpObj[asset_key] = $(".asset-id", this).text();
            tmpObj[asset_text] = $(".asset-val", this).text();
            myArray.push(tmpObj)
            console.log(myArray);

        });

Any help is very appreciated. 任何帮助都非常感谢。 Thanks 谢谢

You have quite a few problems with your code: 您的代码有很多问题:

  1. myArray is defined inside the loop, so you're always instantiating it with an empty array. myArray是在循环内定义的,因此您总是使用空数组实例化它。

  2. The selectors don't work because you're looping over each element. 选择器不起作用,因为您正在遍历每个元素。

  3. Both asset_key and asset_text are empty strings. asset_keyasset_text均为空字符串。

Te following should work for you: 以下应该为您工作:

 var arr = [ ]; $('.asset-id').each(function() { arr.push({ 'id': $(this).text(), 'value': $(this).siblings(".asset-val").text() }); }); console.log( arr ); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <th>Course Name</th> <th>Course ID</th> <th>Value</th> </tr> <tr> <td>Course A</td> <td class="asset-id">1616781</td> <td class="asset-val">1306886</td> </tr> <tr> <td>Course B</td> <td class="asset-id">723507</td> <td class="asset-val">1306886</td> </tr> <tr> <td>Course C</td> <td class="asset-id">723503</td> <td class="asset-val">1306886</td> </tr> </table> 

Instead of looping over the rows and finding each td, you can just select all the elements and loop over that. 无需遍历行并查找每个td,您只需选择所有元素并遍历该行即可。

 var ids = $('.asset-id') var vals = $('.asset-val') var result = []; ids.each(function(i, v){ result.push({ id: +v.textContent, value: +vals.eq(i).text()}) // result.push({ id: +ids.eq(i).text(), value: +vals.eq(i).text()}) }) console.log(result) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tbody> <tr> <th>Course Name</th> <th>Course ID</th> <th>Value</th> </tr> <tr> <td>Course A</td> <td class="asset-id">1616781</td> <td class="asset-val">1306886</td> </tr> <tr> <td>Course B</td> <td class="asset-id">723507</td> <td class="asset-val">1306886</td> </tr> <tr> <td>Course C</td> <td class="asset-id">723503</td> <td class="asset-val">1306886</td> </tr> </tbody> </table> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM