2014-11-03 143 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