1

我有一张桌子。 HTML

<div class="row-fluid">
    <table class="table table-striped" id="table-visual-features">
        <thead>
            <tr>
                <th>Visual Feature</th>
                <th>Type [currently not used]</th>
                <th>Description</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
            <tr><td>A11</td><td>Numeric [1..30]</td><td>Values to be mapped to the S1-axis</td><td><button class="action-change">edit</button> <button class="btn btn-mini btn-danger action-remove-visual-feature">remove</button></td></tr>
            <tr><td>A21</td><td>Numeric [1..10 ]</td><td>Values to be mapped to the S2-axis</td><td><button class="action-change">edit</button> <button class="btn btn-mini btn-danger action-remove-visual-feature">remove</button></td></tr>
            <tr><td>A31</td><td>Numeric [1..20 ]</td><td>Values to be mapped to the S3-axis</td><td><button class="action-change">edit</button> <button class="btn btn-mini btn-danger action-remove-visual-feature">remove</button></td></tr>


        </tbody>
    </table>
</div>

我想将我的值从表转换为这种形式

var features = {
    features: {
        "A11": {"type_feature": "Numeric [1..30]", "description_feature": "Values to be mapped to the S1-axis"},
        "A21": {"type_feature": "Numeric [1..10]", "description_feature": "Values to be mapped to the S2-axis"},
        "A31": {"type_feature": "Numeric [1..20]", "description_feature": "Values to be mapped to the S3-axis"}
    }
};

我取行中第一个单元格的值,它是“A11”。行中第二个单元格的值为“Numeric [1..30]”。行中第三个单元格的值是“要映射到 S1 轴的值”。我们首先进入

features: {
            "A11": {"type_feature": "Numeric [1..30]", "description_feature": "Values to be mapped to the S1-axis"}}

这是我的 jsfiddle http://jsfiddle.net/7q3KP/

请帮我转换价值。先感谢您

4

2 回答 2

4

尝试这个

var features = {features:{}};
$('tr:not(:first)').each(function () {
    var name = $(this).find('td:first').text(); 
    features["features"][name] = {
        "type_feature": $(this).find('td:eq(1)').text(),
        "description_feature": $(this).find('td:eq(2)').text()
    } 
});
console.log(features);

演示

于 2013-10-18T15:21:43.620 回答
2

此代码将遍历行并将数据提取到您的特征对象中:

var features = { features: {}};

$('#table-visual-features tbody tr').each(function() {
     var cells = $(this).find('td');
     features.features[cells.eq(0).text()] = { 
        "type_feature": cells.eq(1).text(),
        "description_feature": cells.eq(2).text()
     };
});

console.log(features);

JSFiddle 示例 - http://jsfiddle.net/Bdv4L/

于 2013-10-18T15:24:04.990 回答