Reputation: 1943
I need to access keys of a json object which was created dynamically . The array structure is:
self.arrayObj : Array[2]
>0:Object
>Display1
->InnerObjects
>__proto
>1:Object
>Display2
-->InnerObjects
Key is "Display1" and value is object. This key is dynamic and not fixed . How can I access the Key string from array above.
The output I am expecting as : "Display1" and "Display2"
Upvotes: 0
Views: 77
Reputation: 42054
Using map you can do:
var arrayObj = [{'Display1': {'InnerObjects': {}}}, {'Display2': {'InnerObjects': {}}}];
var result = arrayObj.map(function(val, index) {
return Object.keys(val)[0];
});
document.write('arrayObj keys: ' + result.toString());
Upvotes: 1
Reputation: 68433
try this
var keys = [];//final output of all key names
arrayObj.forEach(function(val){
keys.concat(Object.keys(val));
});
keys
now has all the dynamic property names.
Upvotes: 2