Is there a way to get the name of the first property of a JSON object?
I\'d like to do something like this:
var firstProp = jsonObj[0];
<
Great question. I don't know of any way besides iterating in a for-in loop. You only need to iterate once though. For safety, ensure it's a known property [more info].
for (var propName in jsonObj) {
if (jsonObj.hasOwnProperty(propName)) {
return propName; // or do something with it and break
}
}
Edited to be extra clear that you're iterating over the property names, not their values.