JSON隐藏“null”数据

时间:2013-08-14 20:22:16

标签: javascript jquery json

我有一个JSON文件,我正在一个bootstrap表中显示。

以下是我用来显示的代码:

$(document).ready(function(){
    $.getJSON(url, function(data){
        content = '<h1><p class="p1"><span class="s1">' + 
                  data.test.name + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>' + 
                  data.test.section + '</td></tr><tr><td>Synonyms:</td><td>' + 
                  data.test.synonyms + '</td></tr><tr><td>Container:</td><td>' + 
                  data.test.container + '</td></tr><tr><td>Reference Ranges:</td><td>' + 
                  data.test.reference + '</td></tr><tr><td>Availability:</td><td>' + 
                  data.test.availability + '</td></tr><tr><td>Special Handling:</td><td>' + 
                  data.test.specialHandling + '</td></tr><tr><td>Additional Comments:</td><td>' + 
                  data.test.additionalComments + '</td></tr></tbody></table>';
            $(content).appendTo("#main");
            $("#main").fadeIn( );

    });
}); 

我想要隐藏任何值=“null”并且困难重重。

2 个答案:

答案 0 :(得分:1)

如果您只想要空值并且没有返回可能的假值,则这将适用于空值和缺失值:

适应小提琴工作

Working Fiddle

var data = {
    test: {
        name: "bob",
        section: null,
        synonyms: "this, that",
        container: "box",
        reference: null,
        availability: null,
        specialHandling: "be careful",
        additionalComments: "No comment"
    }
};

var content = '<h1><p class="p1"><span class="s1">' + 
          (data.test.name || '') + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>' + 
          (data.test.section || '') + '</td></tr><tr><td>Synonyms:</td><td>' + 
          (data.test.synonyms || '') + '</td></tr><tr><td>Container:</td><td>' + 
          (data.test.container || '') + '</td></tr><tr><td>Reference Ranges:</td><td>' + 
          (data.test.reference || '') + '</td></tr><tr><td>Availability:</td><td>' + 
          (data.test.availability || '') + '</td></tr><tr><td>Special Handling:</td><td>' + 
          (data.test.specialHandling || '') + '</td></tr><tr><td>Additional Comments:</td><td>' + 
          (data.test.additionalComments || '') + '</td></tr></tbody></table>';
    $(content).appendTo("#main");
    $("#main").fadeIn( );

版本2

此版本使用scrub功能删除其他值,例如“()”。如果添加了更多条件,那么重新访问擦除逻辑可能是个好主意......

Working fiddle

// Sample dataset
var data = {
    test: { 
        "name": "RNA Polymerase III Antibody", 
        "id": 96800, 
        "container": null, 
        "reference": null,
        "section": null, 
        "synonyms": "()", 
        "availability": null, 
        "specialHandling": null, 
        "additionalComments": null 
    }
};

// Extra assignment kept for clarity
function scrub(inStr) {
    inStr = inStr || '';  // Set to self if it is a truthy value
    inStr = "()" === inStr ? '' : inStr;
    return inStr;
}

var content = '<h1><p class="p1"><span class="s1">' + 
          scrub(data.test.name) + '</span></p></h1><table class="table table-bordered"><tbody><tr><td>Section:</td><td>' + 
          scrub(data.test.section) + '</td></tr><tr><td>Synonyms:</td><td>' + 
          scrub(data.test.synonyms) + '</td></tr><tr><td>Container:</td><td>' + 
          scrub(data.test.container) + '</td></tr><tr><td>Reference Ranges:</td><td>' + 
          scrub(data.test.reference) + '</td></tr><tr><td>Availability:</td><td>' + 
          scrub(data.test.availability) + '</td></tr><tr><td>Special Handling:</td><td>' + 
          scrub(data.test.specialHandling) + '</td></tr><tr><td>Additional Comments:</td><td>' + 
          scrub(data.test.additionalComments) + '</td></tr></tbody></table>';
    $(content).appendTo("#main");
    $("#main").fadeIn( );

答案 1 :(得分:0)

正如已经提到的评论,您没有说出如何输出值 这是应该让你去的东西:

假设我们已返回此数据

var data = {};
data.test = {
    section: "mySection",
    synonyms: "mySyn",
    container: "myContainer",
    reference: undefined,
    availability: "myAvail",
    specialHandling : undefined,
    additionalComments: "myComment"
}

你有一个与data.test

具有相同键的对象
var labels = {};
labels.test = {
    section: "Section",
    synonyms: "Synonyms",
    container: "Container",
    reference: "Reference Ranges",
    availability: "Availability",
    specialHandling: "Special Handling" ,
    additionalComments: "Additional Comments"
}

以下函数可以写入数据

function writeTablerow(labels, data){   
    for (var key in data) {
       if (data.hasOwnProperty(key) && data[key] !== undefined) {
          console.log(labels[key], data[key]);
       }
    }
};

你会这样称呼:

writeTablerow(labels.test, data.test);