2017-06-30 14 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の型(' 'オブジェクト' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ''値がないときは "undefined"となります。 –

+0

それではなぜそれが未定義であるのでしょうか?すべてのキーに値がありますか?ちょうど混乱。 – Paul

関連する問題