-2

I have a java script object in key values, some keys are string and some are array containing objects. I want to find the key of the object and also want to know the type of the key.

GrandParent:{
                'name': '',
                Parent: [{
                    'name': '',
                    Child: [{
                        'name': '',
                        GrandChild: [{
                            'name': 'section',
                        }]
                    }],
                    Child: [{
                        name:''
                    }]
                }],
            }
ozil
  • 6,930
  • 9
  • 33
  • 56
  • Key: A key is always a string enclosed in quotation marks. Ans it is a String type. Have you tried looking for a sulution to your search problem? After 1 min in google I see a result: http://stackoverflow.com/questions/10459917/traversing-through-json-string-to-inner-levels-using-recursive-function – Beri Nov 20 '14 at 07:44

1 Answers1

1

You need something like this:

first define proper variable:

var     GrandParent = { 

...

then

var keyNames = Object.keys( GrandParent );
for ( var i in keyNames )
{
    alert( keyNames[i] );
    alert( type( GrandParent[keyNames[i]] ) )
}

function type( val )
{
    return Object.prototype.toString.call( val ).replace( /^\[object (.+)\]$/, "$1" ).toLowerCase();
}

this will return: name->string and Parent->Array

You can see this solutions:

To find keys: Getting the object's property name

To find types: Better way to get type of a Javascript variable?

Community
  • 1
  • 1
Azzy Elvul
  • 1,403
  • 1
  • 12
  • 22