【问题标题】:How to get keys and their value from a multidimensional object如何从多维对象中获取键及其值
【发布时间】:2017-06-28 20:04:10
【问题描述】:

我试图找出一个键出现了多少个实例,然后我也试图获取它们的值。

假设我计算了密钥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);

【问题讨论】:

    标签: javascript jquery arrays object key


    【解决方案1】:

    您可以将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',
                }
            }
        ]
    };
    

    这是因为您重复了名为 package.. 的键,这将起作用:

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

    【讨论】:

      【解决方案2】:

      这里有几件事。在 Javascript 中,对象属性必须是唯一的。通过将packageContents.packages.package 设置为等于Gold 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',
                  }
              }
          }
      };
      

      注意现在的键是 package#。我的方法很粗糙,但它确实有效。我们可以使用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 + "]");
      

      希望 cmets 消除任何困惑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-04-29
        • 2015-12-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多