-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:''
                    }]
                }],
            }
1
  • 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: stackoverflow.com/questions/10459917/… Commented Nov 20, 2014 at 7:44

1 Answer 1

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?

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

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.