0

I can iterate over an object like this:

var obj = {name: "Fred"}

for(var value in obj) {
    // Key should be "name"
    console.log(key + ": " + value);
}

How can I find out what key is? Is this possible at all? Any help would be appreciated.

hiy
  • 449
  • 5
  • 15
  • 3
    Try your code, what is `value`? – Paul S. Nov 10 '15 at 18:05
  • Oh okay this is embarrassing. – hiy Nov 10 '15 at 18:07
  • Yeah. If you are unfamiliar with a certain construct, such as `for...in`, read the docs first: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in . I mean, how did you even decide to use it if you don't know how it works :P ;) – Felix Kling Nov 10 '15 at 18:11

1 Answers1

4

Hope this will be helpful

var obj = {name: "Fred"}

for(var key in obj) {
    // Key should be "name"
    console.log(key + ": " + obj[key]);
}

jsfiddle

brk
  • 48,835
  • 10
  • 56
  • 78