-1

I have got the above key value pair structure

While looping is it possible to extract only the key

var list_of_stocks= {
  "CBB": "Communications",
  "VZ": "Communications",
  "UPS": "Transportation"
};
for (var key in list_of_stocks) {
console.log(list_of_stocks[0]);
}

So that the output will look like

CBB
VZ
UPS

http://jsfiddle.net/n3fmw1mw/207/

1 Answer 1

3

Just log key:

var list_of_stocks= {
      "CBB": "Communications",
      "VZ": "Communications",
      "UPS": "Transportation",
      "RRTS": "Transportation",
      "AAC": "Heath"
};
for (var key in list_of_stocks) {
  console.log(key);
}

Want an array of keys? Use Object.keys():

var list_of_stocks= {
  "CBB": "Communications",
  "VZ": "Communications",
  "UPS": "Transportation",
  "RRTS": "Transportation",
  "AAC": "Heath"
};

var keys = Object.keys(list_of_stocks);

console.log(keys);
console.log(keys[0]); //CBB

6
  • Thank you , but still is it possible to extract using list_of_stocks ??
    – Pawan
    Jul 29, 2016 at 14:11
  • For example , list_of_stocks[0].1= CBB
    – Pawan
    Jul 29, 2016 at 14:12
  • Can you clarify what you mean? Define extract.
    – Blue
    Jul 29, 2016 at 14:13
  • Do you want an array of keys?
    – Blue
    Jul 29, 2016 at 14:13
  • Similarly is it possible to get values also ?? I mean Communications
    – Pawan
    Jul 29, 2016 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.