Krishna
Krishna

Reputation: 1362

Select Key From Nested object

I have a situation here. I have an object here, Which is nested inside another , here i want to select the key. The object is shown below

var x={
    "_shards": {
        "total": 10,
        "successful": 5,
        "failed": 0
    },
    "_all": {
        "primaries": {
            "indexing": {
                "index_total": 4,
                "index_time_in_millis": 5,
                "index_current": 0,
                "delete_total": 0,
                "delete_time_in_millis": 0,
                "delete_current": 0
            }
        },
        "total": {
            "indexing": {
                "index_total": 4,
                "index_time_in_millis": 5,
                "index_current": 0,
                "delete_total": 0,
                "delete_time_in_millis": 0,
                "delete_current": 0
            }
        }
    },
    "indices": {
        "get56_name": {
            "primaries": {
                "indexing": {
                    "index_total": 4,
                    "index_time_in_millis": 5,
                    "index_current": 0,
                    "delete_total": 0,
                    "delete_time_in_millis": 0,
                    "delete_current": 0
                }
            },
            "total": {
                "indexing": {
                    "index_total": 4,
                    "index_time_in_millis": 5,
                    "index_current": 0,
                    "delete_total": 0,
                    "delete_time_in_millis": 0,
                    "delete_current": 0
                }
            }
        }
    }
}

How can I select the key i.e "get56_name" as answer. I dont want the value, I want to select the key how can i get it?

Upvotes: 0

Views: 219

Answers (2)

Nitish Kumar
Nitish Kumar

Reputation: 4870

Try this:

var your_variable = Object.keys(x.indices)[0];

Upvotes: 1

Sainath Motlakunta
Sainath Motlakunta

Reputation: 945

This will return the first key in array of keys like "get56_name"

Object.keys(x["indices"])[0]

Hope this is what you are asking

Upvotes: 1

Related Questions