简体   繁体   English

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

[英]Javascript Storing Multiple Data Objects In An Array

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. 我不是使用JavaScript或数组的专业人士,因此很有可能我没有提出正确的问题或使用正确的术语。 Anyways here is what I am trying to do. 无论如何,这就是我想要做的。

Using PHP I am able to grab data from googles places API. 使用PHP,我可以从Google Places API中获取数据。 Once I have all the data from my PHP code I want to put that data into a javascript array. 当我从PHP代码中获得所有数据后,我想将该数据放入javascript数组中。 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. 可以说我从Googles API获得5个席位,我希望在数组中有5个条目,每个条目的名称,地址,经纬度,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数组中后,我将运行更多代码以从该数组中随机选择一个“ google位置”,但对于每个随机选择的“位置”,我都需要地址,经纬度和长名称。

Hopefully some one can point me in the right direction. 希望有人可以指出正确的方向。 Thanks. 谢谢。

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. 传递给.push每个参数都作为单个元素添加到数组中。 It's equivalent to 相当于

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文档以了解有关对象的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM