2017-06-30 101 views
-1

我试图检查密钥是否与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); 
    }); 

回答

3

.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); 
 
});

+0

谢谢!所以typeof只是返回未定义或定义? – Paul

+0

@Paul'typeof'将会返回JavaScript类型(''object“'''''''''''''''''''',''''''''''''''' “没有定义”,当没有价值。 –

+0

那么为什么它显示未定义呢?所有的键都有值?只是困惑。 – Paul