Json字符串:检查所有以“a string”开头的键,但所有值都为空

时间:2017-07-13 11:27:28

标签: jquery json

我有这段代码,但这不正确

    var test_h2s = JSON.parse($.cookie('name_of_cookie'));
JSON.stringify(test_h2s, function(key, value) {
// when encoutering one of the keys we're looking for, with all values empty

while(key.substr(0, 3) === 'h2s' && value !== "") {
//do something

}

});

我要检查所有以h2s开头的名称并且所有值都为empty ("h2s...":"")的密钥 恩。在解析后的json中(JSON.stringify) {"h2s":"on","h2s_perc":"dffdfd","other_Content_Outlet":"fdfd"....ecc...ecc}

1 个答案:

答案 0 :(得分:1)

这是循环遍历JSON对象的代码,在IF语句中,您可以访问那些具有空值并以“h2s”开头的键/值对。

var test_h2s = {"h2s":"on","h2s_perc":"dffdfd","other_Content_Outlet":"fdfd","h2s_empty":""};

for (var key in test_h2s) {
    if(key.substr(0,3) === 'h2s' && test_h2s[key] == ""){
        console.log(key); // Prints out the key that has empty value
    };
};