您好我有一个使用JSON.Stringify
输出的对象{"0":["test1","ttttt","","","","","","","",""],"1":["test2","ghjgjhgjh","","","","","","","",""]}
我希望得到这样的输出。
[["test1","ttttt","","","","","","","",""],["test2","ghjgjhgjh","","","","","","","",""]]
我试过这个删除" 0"和" 1"使用.map
var itemjson = $.map(cleanedGridData, function (n) {
return n;
});
然而,这给出了(下面)的输出,它已经变平了。
["test1", "ttttt", "", "", "", "", "", "", "", "", "test2", "ghjgjhgjh", "", "", "", "", "", "", "", ""]
答案 0 :(得分:3)
您可以使用它来提取值:
var res = {"0":["test1","ttttt","","","","","","","",""],"1":["test2","ghjgjhgjh","","","","","","","",""]}
Object.keys(res).map(function(key) {
return res[key];
});
Object.keys将列出初始对象中的所有键。然后,您可以使用map迭代这些键,并在该函数中拉出值。
答案 1 :(得分:0)
更改return语句,将n
放入另一个数组中。
return [n];
这是因为jQuery的$.map
会将返回的数组展平为结果,因此需要将其包装在外部数组中。
或者只是使用for in
循环。
var itemjson = [];
for (var key in cleanedGridData) {
itemjson.push(cleanedGridData[key]);
}
答案 2 :(得分:0)
一旦您的环境(例如,如果您希望支持所有浏览器的浏览器)有Object.values
(和spread operator),您就可以选择
[...Object.values(cleanedGridData)]