2016-03-12 70 views
0

你好,我有一個輸出到這個透過JSON.stringifyJavaScript對象地圖(扁平化)

{"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

更改您的return語句以將n放入另一個數組中。

return [n]; 

這是因爲jQuery的$.map變平將返回到結果數組,所以你需要用它在外部陣列。

要麼是這樣,要麼只是使用for in循環。

var itemjson = []; 
for (var key in cleanedGridData) { 
    itemjson.push(cleanedGridData[key]); 
} 
3

你可以用它來拉出值:

var res = {"0":["test1","ttttt","","","","","","","",""],"1":["test2","ghjgjhgjh","","","","","","","",""]} 
Object.keys(res).map(function(key) { 
    return res[key]; 
}); 

Object.keys將列出所有的鑰匙在你的初始對象。然後,您可以使用map遍歷這些鍵並將該值拉出該函數。

+0

我不喜歡,不需要jQuery的解決方案:) – Sebivor

0

一旦你的環境(在瀏覽器的情況下,即要支持所有瀏覽器)已經Object.values(和spread operator)可用,你可以去

[...Object.values(cleanedGridData)]