【问题标题】:JSON get key/value: if non-existing, JS just stop. How to workaround to set the array?JSON 获取键/值:如果不存在,JS 就停止。如何解决设置数组?
【发布时间】:2017-06-01 03:44:10
【问题描述】:

给定 localStorage 中的一个大型 JSON 表和用户提供的给定键,我访问一个关联的值。但是如果值和/或键不存在,那么我想创建它们。不过……

鉴于以下 JSON:

var data = [ 
{ 'myKey': 'A', 'status': 0 },
{ 'myKey': 'B', 'status': 1 },
{ 'myKey': 'C' },
{ 'myKey': 'D', 'status': 1 }
];

以及以下JS:

function getJsonVal(json, itemId) {
    for (var i in json) {
        if (json[i].myKey == itemId) {
            return json[i]; 
        }
    }
}

如果我...

// request non-existing-in-JSON value:
valC = getJsonVal(data, 'C');
alert("this is C's value: "+ valC)

// request non-existing-in-JSON key:
keyE = getJsonVal(data, 'E');
alert("this is E: "+ keyE);

脚本中途停止。

我希望有一些错误值允许我制作类似If ( null|| undefined ) Then create new key/value 的东西,但我的脚本只是停止,因为这些项目不存在。有什么解决方法吗? Jsfiddle 不胜感激。

【问题讨论】:

    标签: javascript jquery json


    【解决方案1】:

    使用 typeof 运算符,您可以安全地检查是否设置了属性

    function getJsonVal(json, itemId) {
        for (var i in json) {
            if (typeof json[i].myKey != 'undefined' && json[i].myKey == itemId) {
                return json[i]; 
            }
        }
        return 'someDefault';
    }
    

    【讨论】:

    • 如果我理解得很好,这只是为了检查属性是否设置。对吗?
    【解决方案2】:

    我对您的示例代码的修改:

    http://jsfiddle.net/dqLWP/

    var data = [ 
    { 'myKey': 'A', 'status': 0 },
    { 'myKey': 'B', 'status': 1 },
    { 'myKey': 'C' },
    { 'myKey': 'D', 'status': 1 }
    ];
    
    function getJsonVal(json, itemId) {
        for (var i in json) {
            if (json[i].myKey == itemId) {
                return json[i]; 
            }
        }
    }
    
    var output = getJsonVal(data, 'E');
    alert("this is the outputted value: "+ output);
    
    if ( ! output) {
        alert('time to create that new key/value you wanted');
    }
    

    【讨论】:

    • 简单英语中的(!输出)是什么意思?
    • Solution here @Lyndon:我希望接受您的回答,请用简单的英语澄清“(!输出)”并在您的回答的 IF 后面附加: data.push({'myKey': 'E ', '状态': 1});
    • "!"否定一个表达式。 “如果(!真)”基本上意味着“如果(假)”更多:developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/…
    • 所以 !output 的意思是“如果输出不存在”或“未定义”,我明白了。谢谢。
    【解决方案3】:

    除非您在某处为变量 CE 设置值,否则您需要将它们放在引号中:

    // request non-existing-in-JSON value:
    valC = getJsonVal(data, 'C');
    alert("this is C's value: "+ valC)
    
    // request non-existing-in-JSON key:
    keyE = getJsonVal(data, 'E');
    alert("this is E: "+ keyE);
    

    此外,您应该提醒您设置的变量(valCkeyE)。

    【讨论】:

    • 拍得好!完毕。谢谢杰夫。
    【解决方案4】:

    改进林登的回答:

    function getJsonVal(json, itemId) {
        for (var i in json) {
            if (json[i].myKey == itemId) {
                return json[i].status; // add .status here so you can see the value of keys that exist and have one.
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-18
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多