如何从json中删除null对象

时间:2014-10-28 07:17:31

标签: javascript jquery json

我是Bootstrap JS的新手。我有一个用Bootsrap JS制作的表,其数据来自Json文件。这是代码 -

<div class="container-fluid">
     <div class="row">
         <div class="col-md-8">
             <div class="fixedscroll">
                 <table id="user_table" class="table table-hover table-bordered table-striped responsive" style="margin-bottom: 0;" class="display">
                    <thead>
                        <tr>                                
                            <th>UID</th>
                            <th>Name</th>
                            <th>Address</th>
                            <th>Tags</th>   
                            <th>Edit tags</th>                      
                        </tr>
                    </thead>
                 </table>
             </div>
        </div>
    </div> 

function showAll(){
   $.ajax({
      url: "showAll",
      dataType:"json",
      success:function(data){
            $('#user_table tr:gt(0)').remove();
            jQuery.each(data['Payload'], function(index, value) {
            var row = '<tr>'                            
                   + '<td id="tduid">'+ value['uid']+ '</td>'
                   + '<td>'+ value['name']+ '</td>' 
                   + '<td>'+ value['address']+ '</td>'
                   + '<td>'+ value['tag1']+ ',' + value['tag2']+ ',' + value['tag3']+'</td>' + '<td>'+ '<button class="deleteUser btn btn-danger" type="submit">Edit</button>' + '</td></tr>';

            $('#user_table').append(row);
      }); 
     }
});

现在这个Payload是我的json的名字,它来自我调用数据库的servlet。

现在让我们有3个标签。但有些行有2个标签。因此,当我将值放在json中时,Json看起来像 -

{"Payload":[{"uid":"u01","name":"Subho","address":"Dumdum","tag1":"aircel","tag2":"vodafone","tag3":"airtel"},{"uid":"u02","name":"Jeet","address":"Baruipur","tag1":"airtel","tag2":"","tag3":"aircel"},{"uid":"u03","name":"Diba","address":"Jadavpur","tag1":"vodafone","tag2":"aircel","tag3":"airtel"},{"uid":"u04","name":"Tommy","address":"Baguihati","tag1":"aircel","tag2":"vodafone","tag3":""},{"uid":"u05","name":"Jonty","address":"Rahara","tag1":"","tag2":"vodafone","tag3":"airtel"},{"uid":"u06","name":"Gourav","address":"Tripura","tag1":"aircel","tag2":"vodafone","tag3":"airtel"}]}

现在您可以看到,对于UID = U02,有2个标签。输出看起来像图片附加。如何删除空值或空值????请有人帮助我...... enter image description here

1 个答案:

答案 0 :(得分:2)

我想,你是在谈论标签栏中的额外, ......一个凌乱的解决方案是

$.ajax({
    url: "showAll",
    dataType: "json",
    success: function (data) {
        $('#user_table tr:gt(0)').remove();
        jQuery.each(data['Payload'], function (index, value) {
            var tags = $.map([value.tag1, value.tag2, value.tag3], function (value) {
                return value || undefined;
            });

            var row = '<tr>' + '<td id="tduid">' + value['uid'] + '</td>' + '<td>' + value['name'] + '</td>' + '<td>' + value['address'] + '</td>' + '<td>' + tags + '</td>' + '<td>' + '<button class="deleteUser btn btn-danger" type="submit">Edit</button>' + '</td></tr>';

            $('#user_table').append(row);
        });
    }
});

正如@Alnitak所说

var tags = [value.tag1, value.tag2, value.tag3].filter(function (value) {
    return value !== undefined;
});
console.log(tags)