使用Array.each从JSON查询获取数据到javascript数组中

时间:2022-12-08 09:47:19

I have results from a Google Geocoder request and I need a value form them to go into another array as follows:

我有来自Google Geocoder请求的结果,我需要一个值表单,然后进入另一个数组,如下所示:

var data = {};

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

I need data to then contain this structure:

我需要数据然后包含这个结构:

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

An example of the JSON results from the query here:

此处查询的JSON结果示例如下:

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

In the case of the example query above the output I want is:

在上面的示例查询的情况下,我想要的输出是:

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

Im quite confused about what needs to happen in the Array.each function.

我很困惑在Array.each函数中需要发生什么。

Any help would be great, thanks.

任何帮助都会很棒,谢谢。

1 个解决方案

#1


1  

Assuming results contains the results array inside the JSON object above:

假设结果包含上面JSON对象中的结果数组:

var data = [];

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

If results contains the whole JSON object though then you need to write:

如果结果包含整个JSON对象,那么你需要写:

results = results.results;

before that loop.

在那个循环之前。

#1


1  

Assuming results contains the results array inside the JSON object above:

假设结果包含上面JSON对象中的结果数组:

var data = [];

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

If results contains the whole JSON object though then you need to write:

如果结果包含整个JSON对象,那么你需要写:

results = results.results;

before that loop.

在那个循环之前。