简体   繁体   中英

How to get keys and their value from a multidimensional object

I am trying to find how many instances a key shows up for and then I am trying to obtain their values as well.

Let's say I was the count how many times the key tpoint shows up in packageContents . Then I am trying to get a comma separated list of the touches from each tpoint . Something like this:

tpInstance = 2;

[tpoint = 21, tpoint = 9]

Does anyone know how I can obtain this?

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

you can change your packageContents structure to:

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

this is because you have repeated keys named package .. and this will make the work:

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

So a few things here. In Javascript, object properties need to be unique. By setting packageContents.packages.package equal to the Gold Bundle Package and then setting the same property to the Bronze Bundle Package you effectively overwrite the original property. Like the other answer says, you can rewrite your object like so:

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

Note the keys are now package . My approach is gross, but it works. We can use Object.keys() to figure out the structure of the object and iterate over its values. This is my approach:

// 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 + "]");

Hopefully the comments clear up any confusion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM