jquery - best way to get the key of a key/value javascript object -
if have js object like:
var foo = { 'bar' : 'baz' } if know foo has basic key/value structure, don't know name of key, what's easiest way it? for ... in? $.each()? hope there's better....
if want keys, ecmascript 5 introduced object.keys. supported newer browsers mdc documentation provides alternative implementation (which uses for...in btw):
if(!object.keys) object.keys = function(o){ if (o !== object(o)) throw new typeerror('object.keys called on non-object'); var ret=[],p; for(p in o) if(object.prototype.hasownproperty.call(o,p)) ret.push(p); return ret; } of course if want both, key , value, for...in reasonable solution.
Comments
Post a Comment