【问题标题】:Checking if keys have values检查键是否有值
【发布时间】:2017-12-04 17:02:00
【问题描述】:

我正在尝试检查 calendartpoint 键是否具有与之关联的值。

起初我试图检查是否包含 calendartpoint 键,但后来我意识到它们始终是键。我想检查calendartpoint 键是否具有值的原因是因为有时它们不会。

我的尝试位于下面的packageContents 对象之下。

有谁知道我如何检查键是否有值?

var packageContents = {
        'packages': [
            {
                'price': '32',
                'name': 'Gold Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': 'Gold',
                    'touches': '21',
                    'years': '7',
                }
            },
            {
                'price': '23',
                'name': 'Bronze Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': 'Bronze',
                    'touches': '9',
                    'years': '7',
                }
            }
        ]
    };

packageContents['packages'].forEach(function (bun) {
    if (bun['calendar'].length >= 1 && bun['tpoint'].length >= 1) {
        var bundleSet;
        console.log(bundleSet);
        console.log('Working');
    }
});

编辑:

var packageContents = {
        'packages': [
            {
                'price': '23',
                'name': 'Bronze Bundle Package',
                'calendar': {
                    'type': '2year',
                    'color': 'Brushed Nickel',
                },
                'tpoint': {
                    'type': '',
                    'touches': '',
                    'years': '',
                }
            }
        ]
    };




var bundleSet = '';

    packageContents['packages'].forEach(function (obj) {
        if (typeof obj.calendar !== 'undefined' && typeof obj.tpoint !== 'undefined') {
            bundleSet = "Bundle";

        }
        else {
            bundleSet = "not bundle"
        }
        console.log(bundleSet);
    });

【问题讨论】:

    标签: javascript jquery arrays object key


    【解决方案1】:

    您的.forEach() 循环正在循环packageContent.packages 数组中的对象,因此最好使用变量名来提醒您这一点。

    然后,由于您知道这些对象中键的静态名称,因此您无需使用括号表示法 ([]) 将它们作为字符串传递给对象,您可以简单地在实例上使用点表示法。

    最后,您只想检查这些键中是否存储了值,因此可以检查存储在那里的数据类型是否不是undefined

    var packageContents = {
            'packages': [
                {
                    'price': '32',
                    'name': 'Gold Bundle Package',
                    'calendar': {
                        'type': '2year',
                        'color': 'Brushed Nickel',
                    },
                    'tpoint': {
                        'type': 'Gold',
                        'touches': '21',
                        'years': '7',
                    }
                },
                {
                    'price': '23',
                    'name': 'Bronze Bundle Package',
                    'calendar': {
                        'type': '2year',
                        'color': 'Brushed Nickel',
                    },
                    'tpoint': {
                        'type': '',
                        'touches': '',
                        'years': '',
                    }
                }
            ]
        };
    
    var bundleSet =  null;
    packageContents.packages.forEach(function (obj) {
    
      // We are now looping over each top-level object in the main array.
      // To see if each of the properties in quesiton of those objects have values,
      // we need to loop over those properties
      for(var prop in obj){
        
        // We're only interested in "calendar" and "tpoint"
        if(prop === "calendar" || prop === "tpoint"){
          
          // These properties store objects of their own and it is these properties we need to check
          for(var subProp in obj[prop]){
        
            // Since you know the key names, you can access them directly on the instance 
            // and simply see if they have values by checking that they are not undefined
            if (obj[prop][subProp] !== "") {
              bundleSet = true;
            } else {
              bundleSet = false;
            }
          }
        }
    
      }
      console.log(bundleSet);
    });

    【讨论】:

    • 谢谢!那么 typeof 只是返回未定义或已定义?
    • @Paul typeof 将返回 JavaScript 类型("object""string""number""boolean""function" 等),否则将返回 "undefined"没有价值。
    • 那为什么显示未定义呢?所有的键都有值吗?只是困惑。
    • @Paul 因为,在你的代码中,你声明了var bundleSet,但是你从来没有将它初始化为任何东西,然后你立即记录它。那部分与实际问题或解决方案无关,只是我认为你有某种目的的额外代码。
    • @Paul 我完全误解了你所追求的,我已经更新了我的答案以反映你的问题。在您的情况下,如果您希望属性有时可能包含空字符串(即'years': ''),那么只需要对空字符串进行简单测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    • 2013-12-15
    • 1970-01-01
    • 2015-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多