How to access key itself using javascript

后端 未结 3 1164
忘掉有多难
忘掉有多难 2020-12-21 06:26

I have a JSON like:

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

Now I can read the value of name which is \'alx\' as xx[0].name, but how shou

相关标签:
3条回答
  • 2020-12-21 06:46

    You are looking for associative arrays in Javascript. A quick google search suggests the following:

    Read this page http://www.quirksmode.org/js/associative.html

    and especially this section http://www.quirksmode.org/js/associative.html#link5

    0 讨论(0)
  • 2020-12-21 06:56

    modified Code (from Victor) taking into account that you might want to look for any other possible string

    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
        }
    }
    
    0 讨论(0)
  • 2020-12-21 07:01
    for (i in xx) {
        if (xx[i] == "alx") {
            // i is the key
        }
    }
    
    0 讨论(0)
提交回复
热议问题