0

我从服务器字符串中获取如下信息:

{"Point Ref":[15629989,564646414,65494949],
"Effective Date":["2008-03-03","2010-12-14","2004-10-01"],
"Identifier":["EM","EM","SC"],"Status":["FI","SI","XC"]}"

我转换为JSON

var jsonResponse = jQuery.parseJSON(xmlHttp.responseText.trim());

直到这里一切都很好:我可以循环jsonResponse并做我的事情。但是,我找不到一种简单快捷的方法来立即将所有密钥合二为一array

基本上,除此之外还有什么:

var keys = new Array();
var n = 0
for (var i in jsonResponse){
    keys[n] = i
    n ++
}

谢谢。

4

2 回答 2

6

您可以使用Object.keys

var keys = Object.keys(jsonResponse);
于 2013-08-12T12:13:00.153 回答
0

使用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; });
于 2013-08-12T12:16:30.587 回答