在数组中存储多个数据对象的Javascript

时间:2021-11-11 22:19:24

I am not a pro with javascript or arrays so there is a good chance I am not asking the right question or using the correct terminology. Anyways here is what I am trying to do.

我不是javascript或数组专家,所以很有可能我没有问正确的问题或使用正确的术语。总之,这就是我要做的。

Using PHP I am able to grab data from googles places API. Once I have all the data from my PHP code I want to put that data into a javascript array. Lets say I get 5 places from googles API I would want 5 entries in the array and each entry with name, address, lat, lng.

使用PHP,我可以从google places API获取数据。一旦我有了PHP代码中的所有数据,我就想把这些数据放到一个javascript数组中。假设我从google API中得到5个位置我希望数组中有5个条目,每个条目都有名称,地址,lat, lng。

I tried to use this code for creating the array but it doesn't create 5 entries in the array, it creates 1 entry in the array for each data object.

我尝试使用这个代码来创建数组,但是它没有在数组中创建5个条目,它为每个数据对象在数组中创建1个条目。

var data = []; 
            data.push({name:"BotanaCare"}, {lat:"39.904374"}, {lng:"-104.990527"}, {address:"11450 Cherokee Street A7, Northglenn"});
            data.push({name:"The Green Solution"}, {lat:"39.901838"}, {lng:"-104.979542"}, {address:"Malley Heights, 470 Malley Drive, Northglenn"});
            data.push({name:"Doc's Apothecary"}, {lat:"39.897978"}, {lng:"-104.963226"}, {address:"2100 East 112th Avenue, Northglenn"});
            data.push({name:"Emerald City"}, {lat:"39.789952"}, {lng:"-105.025937"}, {address:"5115 Federal Boulevard, Denver"});
            data.push({name:"La Conte's Clone Bar and Dispensary"}, {lat:"39.790864"}, {lng:"-104.977946"}, {address:"5194 Washington Street, Denver"});

Once I have the data in the proper javascript array I will be running some more code to randomly pick a "google place" from that array but for each randomly picked "place" i need the address, lat, long, and name.

一旦我在适当的javascript数组中有了数据,我将运行更多的代码,以便从该数组中随机选择一个“谷歌位置”,但是对于每个随机选择的“位置”,我都需要地址、lat、long和name。

Hopefully some one can point me in the right direction. Thanks.

希望有人能给我指明正确的方向。谢谢。

1 个解决方案

#1


4  

data.push({name:"BotanaCare"}, {lat:"39.904374"}, {lng:"-104.990527"}, {address:"11450 Cherokee Street A7, Northglenn"});

creates and adds four objects to the array. Every argument you pass to .push is added as individual element to the array. It's equivalent to

创建并向数组添加四个对象。将传递给.push的每个参数作为单个元素添加到数组中。它等于

data.push({name:"BotanaCare"}); // object with only property name
data.push({lat:"39.904374"});   // object with only property lat
// ...

If you only want to push a single object, then you have to create a single object (I changed the formatting to make it more readable):

如果你只想推一个对象,那么你必须创建一个对象(我改变了格式,使它更可读):

data.push({ // object with properties name, lat, lng and address
    name:"BotanaCare",
    lat:"39.904374",
    lng:"-104.990527",
    address:"11450 Cherokee Street A7, Northglenn"
});

Have a look at the MDN documentation to learn more about objects.

查看MDN文档了解更多关于对象的信息。

#1


4  

data.push({name:"BotanaCare"}, {lat:"39.904374"}, {lng:"-104.990527"}, {address:"11450 Cherokee Street A7, Northglenn"});

creates and adds four objects to the array. Every argument you pass to .push is added as individual element to the array. It's equivalent to

创建并向数组添加四个对象。将传递给.push的每个参数作为单个元素添加到数组中。它等于

data.push({name:"BotanaCare"}); // object with only property name
data.push({lat:"39.904374"});   // object with only property lat
// ...

If you only want to push a single object, then you have to create a single object (I changed the formatting to make it more readable):

如果你只想推一个对象,那么你必须创建一个对象(我改变了格式,使它更可读):

data.push({ // object with properties name, lat, lng and address
    name:"BotanaCare",
    lat:"39.904374",
    lng:"-104.990527",
    address:"11450 Cherokee Street A7, Northglenn"
});

Have a look at the MDN documentation to learn more about objects.

查看MDN文档了解更多关于对象的信息。