2017-06-28 18 views
0

キーが表示されているインスタンスの数を調べようとしていて、その値も取得しようとしています。多次元オブジェクトからキーとその値を取得する方法

たとえば、tpointのキーが何度表示されたのかを、packageContentsとしましょう。次に、それぞれtpointからカンマ区切りのtouchesのリストを取得しようとしています。このようなもの:

tpInstance = 2;

[tpoint = 21、tpoint = 9]

誰も私がこれを入手することができます方法を知っていますか?

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

    var tpInstance = Object.keys(package).length; 
    console.log(tpInstance); 

答えて

1

あなたがあなたのpackageContents構造を変更することができます:あなたはpackageという名前のキーを繰り返してきたので、これは..です、これは作業を行います

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', 
      } 
     } 
    ] 
}; 

var tpInstance = 0; 
var result = []; 
packageContents.packages.map(function(pack) { 
    if('tpoint' in pack) { 
     if('touches' in pack.tpoint) { 
      result.push(pack.tpoint.touches); 
      tpInstance ++; 
     } 
    } 
}); 
0

ので、いくつかここのものJavascriptでは、オブジェクトのプロパティは一意である必要があります。 packageContents.packages.packageGold Bundle Packageに設定し、同じプロパティをBronze Bundle Packageに設定すると、元のプロパティを効果的に上書きできます。他の答えが言うように、あなたはそのようなあなたのオブジェクトを書き換えることができます。

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

注キーは今パッケージです。私のアプローチは総額ですが、それは機能します。 Object.keys()を使用してオブジェクトの構造を把握し、その値を反復処理することができます。これは私のアプローチです:

// The number of tPoitn objects found 
var total_tPoints = 0; 

// The values of the "touches" value within the tPoint objects 
var tPoint_touches = []; 

// Returns an array of the keys of "package" === ["package", "package1"] 
var packageContentKeys = Object.keys( packageContents[ Object.keys(packageContents)[0] ] ); 

// Loop through that array 
for (var i = 0; i < packageContentKeys.length; i++) { 

    // Get the individual "package#" object 
    //      base object    "packages"     "package1", "package2", etc 
    var individual_package = packageContents[ Object.keys(packageContents)[0] ] [ packageContentKeys[i] ]; 

    // Get the tPoint object 
    var individual_tPoint = individual_package.tpoint; 

    // If it exists, then we do stuff 
    if (individual_tPoint !== undefined) { 

     // Increment the total number of tPoint objects 
     total_tPoints++; 

     // Push a string with the value of the touches value 
     tPoint_touches.push("tPoint" + " = " + individual_tPoint.touches); 
    } 

} 

// Print the stuff 
console.log("Total tPoints: " + total_tPoints); 
console.log("tPoint Array: [" + tPoint_touches + "]"); 

コメントは混乱を避けることを望みます。

関連する問題