2014-11-03 178 views
-2

MySQL查詢後,我用了json_encode將結果轉換查詢和我得到以下幾點:轉換JSON對象到JavaScript數組

[ 
    {"id":"1","map_id":"1","description":"This is Athens","lat":"37.77994127700315","lng":"23.665237426757812","title":"Athens"}, 
    {"id":"2","map_id":"1","description":"This is Rome","lat":"41.9100711","lng":"12.5359979","title":"Rome"} 
] 

我想將其轉換爲JavaScript數組,但得到只有價值。例如:

myArray = [ 
    [1, 1, 'This is Athens', 37.77994127700315,23.665237426757812, 'Athens'] 
    [2, 1, 'This is Rome', 41.9100711, 12.5359979, 'Rome'] 
] 

我嘗試了許多解決方案,我發現這裏的,但我沒有找到任何解決辦法給我酷似myArray數組。

+2

[?你嘗試過什麼(http://mattgemmell.com/what -have-you-tried /) – 2014-11-03 13:30:15

+0

你可以看看http://www.json.org/js.html – 2014-11-03 13:30:43

+0

我試過解決方案在我發現這裏http://stackoverflow.com/questions/20881213/converting-json-object-into-javascript-array和在一些其他問題。 – 2014-11-04 08:08:14

回答

0

假設:

var a = [{"id":"1","map_id":"1","description":"This is Athens","lat":"37.77994127700315","lng":"23.665237426757812","title":"Athens"},{"id":"2","map_id":"1","description":"This is Rome","lat":"41.9100711","lng":"12.5359979","title":"Rome"}]; 

您可以使用Array.prototype.map()

var myArray = a.map(function(e){ 
    return [e.id, e.map_id, e.description, e.lat, e.lng, e.title]; 
}); 

結果:

[ 
    ["1","1","This is Athens","37.77994127700315","23.665237426757812","Athens"], 
    ["2","1","This is Rome","41.9100711","12.5359979","Rome"] 
] 
+0

如何檢查myArray的內容?我用alert(JSON.stringify(myArray))。這是正確的嗎? – 2014-11-03 13:44:24

+0

是的,雖然我喜歡使用console.log(myArray)' – Cerbrus 2014-11-03 13:46:42

+0

開發人員控制檯我收到以下錯誤:Uncaught TypeError:undefined不是一個函數爲線var myArray = a.map(function( e){ – 2014-11-03 14:00:15