如果key = 0的值,如何从JSON文件中跳过对象?

时间:2014-10-27 20:47:23

标签: jquery

我有一个从JSON文件填充的表,到目前为止一切正常,但我正在尝试跳过名为“member_count”的键的值为0的对象。这可能吗?

这是代码:

$mygroupstable = $('#my_groups_table');
    $.ajax({
    url: 'http://testingsite.com/jsondata/mygroups.json',
    dataType:'json',
    success:function(data){
        $.each(data.groups, function(key, val){
            $mygroupstable.append('<tr><td><input type="checkbox" class="groupselector" member_count="' + val.member_count + '" value="' + val.value + '" id="' + val.id + '" name="' + val.name + '" group_name="' + val.group_name + '" /></td><td style="width:10px;"></td><td><label for="' + val.id + '">' + val.group_name + '</label></td></tr>');
        })
    },
});

JSON文件如下所示:

{
"groups": [
    {
        "id": "data1",
        "member_count": "1",
        "value": "255",
        "name": "data[]",
        "group_name": "Group 1"
    },
    {
        "id": "data2",
        "member_count": "5",
        "value": "256",
        "name": "data[]",
        "group_name": "Group 2"
    },
    {
        "id": "data3",
        "member_count": "4",
        "value": "257",
        "name": "data[]",
        "group_name": "Group 3"
    },
    {
        "id": "data4",
        "member_count": "6",
        "value": "258",
        "name": "data[]",
        "group_name": "Group 4"
    },
    {
        "id": "data5",
        "member_count": "0",
        "value": "259",
        "name": "data[]",
        "group_name": "Group 5"
    }
]}

在这种情况下,不应将第5组附加到表中。非常感谢任何帮助!!

3 个答案:

答案 0 :(得分:1)

检查成员数是否为零?

$mygroupstable = $('#my_groups_table');
    $.ajax({
    url: 'http://testingsite.com/jsondata/mygroups.json',
    dataType:'json',
    success:function(data){
        $.each(data.groups, function(key, val){
            if(val.member_count !== 0){
                $mygroupstable.append('<tr><td><input type="checkbox" class="groupselector"  member_count="' + val.member_count + '" value="' + val.value + '" id="' + val.id + '" name="' + val.name + '" group_name="' + val.group_name + '" /></td><td style="width:10px;"></td><td><label for="' + val.id + '">' + val.group_name + '</label></td></tr>');
            }
        })
    },
});

答案 1 :(得分:1)

试试这个:

var $mygroupstable = $('#my_groups_table');
var url = 'http://testingsite.com/jsondata/mygroups.json.json';
$.getJSON(url, function(data) {
    $.each(data.groups, function(key, val) {
        if (val.member_count !== "0") {
            $mygroupstable.append('<tr><td><input type="checkbox" class="groupselector" member_count="' + val.member_count + '" value="' + val.value + '" id="' + val.id + '" name="' + val.name + '" group_name="' + val.group_name + '" /></td><td style="width:10px;"></td><td><label for="' + val.id + '">' + val.group_name + '</label></td></tr>');
        }
    });
});

答案 2 :(得分:0)

我会测试密钥是否为成员计数,并且在附加表之前值为0。

$mygroupstable = $('#my_groups_table');
    $.ajax({
    url: 'http://testingsite.com/jsondata/mygroups.json',
    dataType:'json',
    success:function(data){
        $.each(data.groups, function(key, val){
            if ( val.member_count !== 0 ) {
                $mygroupstable.append('<tr><td><input type="checkbox" class="groupselector" member_count="' + val.member_count + '" value="' + val.value + '" id="' + val.id + '" name="' + val.name + '" group_name="' + val.group_name + '" /></td><td style="width:10px;"></td><td><label for="' + val.id + '">' + val.group_name + '</label></td></tr>');
            }
        })
    },
});