How to access the key itself using javascript

I have a JSON:

  var xx = {'name':'alx','age':12};

Now I can read the value of name, which is "alx" as xx [0] .name, but how do I get the value of the name itself? By this I mean, how can I get the key at runtime?

+3
source share
3 answers
for (i in xx) {
    if (xx[i] == "alx") {
        // i is the key
    }
}
+2
source

modified code (from Victor), given that you can search for any other possible line

var search_object = "string_to_look_for";
for (i in xx) {
    if (xx[i] == search_object) {
        // i is the key 
        alert(i+" is the key!!!"); // alert, to make clear which one
    }
}
+1
source
0
source

Source: https://habr.com/ru/post/1747827/


All Articles