user1400915
user1400915

Reputation: 1943

Accessing dynamic keys from a json object of an array

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

Answers (2)

gaetanoM
gaetanoM

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

gurvinder372
gurvinder372

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

Related Questions