1

Lets suppose I have this object

var cliCorp14={
  "id":13,
  "url":"corp_longboard.html",
  "logo":"img/corp/logoLongboard.png",
  "alt":"Longboard store",
  "name":"LONGBOARD",
};

I want to put the text "alt" in a var. I don't want to put "Longboard store", I want to put the text "alt". How do I access to that object?

depperm
  • 10,606
  • 4
  • 43
  • 67
mvdlucas
  • 45
  • 5
  • well write `var whatever='alt'`, or can 'alt' change, if it can how do you identify which key to store? – depperm Jul 22 '15 at 18:16
  • possible duplicate of [How do I enumerate the properties of a JavaScript object?](http://stackoverflow.com/questions/85992/how-do-i-enumerate-the-properties-of-a-javascript-object) – Mike Cluck Jul 22 '15 at 18:20
  • That's the problem, 'alt' changes. Thank you btw! – mvdlucas Jul 22 '15 at 18:35

2 Answers2

1
for(var x in cliCorp14)
{
console.log(x);
}
0
Object.keys(cliCorp14);
// -> ["id", "url", "logo", "alt", "name"]

Note that Object.keys() is an ES5+ method, and only available in IE9+ (although it is easy to polyfill).

jmar777
  • 38,796
  • 11
  • 66
  • 64
  • I think this is the best solution. So, this Object.keys() method only works in IE9? It doesn't works in chrome or mozilla? Thank you for your time – mvdlucas Jul 22 '15 at 18:37
  • No, it'll work in all relevant versions of Firefox, Chrome, Safari, etc. Basically just <=IE8 are the only (potentially) relevant browsers that won't support it. See [this page](http://kangax.github.io/compat-table/es5/#Object.keys) for more compatibility information. – jmar777 Jul 22 '15 at 18:39
  • Ok, im new in this javascript thing, sorry if all of this is basic. Thank you again! – mvdlucas Jul 22 '15 at 18:41
  • No apology necessary. If we were all pros at every possible topic, we wouldn't need SO. :) – jmar777 Jul 22 '15 at 18:44