Reputation: 470
I get from the server strings like the following:
{"Point Ref":[15629989,564646414,65494949],
"Effective Date":["2008-03-03","2010-12-14","2004-10-01"],
"Identifier":["EM","EM","SC"],"Status":["FI","SI","XC"]}"
which I convert to JSON
with:
var jsonResponse = jQuery.parseJSON(xmlHttp.responseText.trim());
Until here everything is fine: I can loop through jsonResponse
and do my stuff. However I can't find an easy quick way to have immediately all the key in one array
.
Basically, is there anything else other than this:
var keys = new Array();
var n = 0
for (var i in jsonResponse){
keys[n] = i
n ++
}
Thanks.
Upvotes: 0
Views: 1398
Reputation: 35213
An alternative solution using array.map()
//minified polyfill for old browsers:
if (!Array.prototype.map) { Array.prototype.map = function (e, t) { var n, r, i; if (this == null) { throw new TypeError(" this is null or not defined") } var s = Object(this); var o = s.length >>> 0; if (typeof e !== "function") { throw new TypeError(e + " is not a function") } if (t) { n = t } r = new Array(o); i = 0; while (i < o) { var u, a; if (i in s) { u = s[i]; a = e.call(n, u, i, s); r[i] = a } i++ } return r } }
var keys = jsonResponse.map(function(v, k) { return k; });
Upvotes: 0