0

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"

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.