2011-08-15 90 views
0

我从谷歌地理编码器请求的结果,我需要一个值形成他们去到另一个数组如下:获取从JSON查询数据到JavaScript数组Array.each

var data = {}; 

Array.each(results, function(loc) 
{ 
    data.['value'] = loc.formatted_address; 
} 

我需要的数据,然后包含此结构:

http://maps.googleapis.com/maps/api/geocode/json?address=new%20york&sensor=false

data = [ 
    {value: 'location one'}, 
    {value: 'location two'}, 
    {value: 'location three'} 
]; 

从查询这里的JSON结果的例子

在我需要的输出上面的例子中查询的情况是:

data = [ 
    {value: 'New York, NY, USA'}, 
    {value: 'Manhattan, New York, NY, USA'} 
]; 

林很困惑什么需要在Array.each功能的情况发生。

任何帮助将是伟大的,谢谢。

回答

1

假设结果包含了JSON对象中的结果阵列之上:

var data = []; 

for(i = 0; i < results.length; i++) 
    data.push({'value': results[i].formatted_address}); 

如果结果包含整个JSON对象,但随后你需要写:

results = results.results; 

该循环之前。

+0

我的救星! = D谢谢! :D <3 – Nicekiwi