1

Given a json such :

 var WNES_list = {
     "India":{ "W": 67.0, "N":37.5, "E": 99.0, "S": 5.0, "vert_%": 106 },
     };

I know how to access values :

var myValue = WNES_list.India.W;

How to access to a key such W, or India ?

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • What do you mean "access to a key"? Do you want to iterate over the key names? In this case, use a [`for..in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loop. – bfavaretto Sep 11 '13 at 17:29
  • 1
    Not to be nitpicky but that isn't JSON, it's object literal notation. – m90 Sep 11 '13 at 17:29
  • I want to get the key, literally "India". – Hugolpz Sep 11 '13 at 17:35
  • @Sushanth's answer does just that, have you tried it yet? – m90 Sep 11 '13 at 17:38
  • Sush's answer iterate through the values. I'am looking for a pathway, similar to what is used for values, in ex: `WNES_list.India.W` – Hugolpz Sep 11 '13 at 17:42
  • You could always go and steal the parts used for underscore's `.where()` which should be what you are looking for if I understand you correctly: http://underscorejs.org/#where – m90 Sep 11 '13 at 17:51

1 Answers1

1

You can use a for loop to access the key

for (var key in WNES_list) {
    var obj = WNES_list[key];
    console.log(key); // logs : India, France
    for (var k in obj) {
        if (obj.hasOwnProperty(k)) {
            console.log(k);
        }
    }
}

Check Fiddle

Sushanth --
  • 55,259
  • 9
  • 66
  • 105