2

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.

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.

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.

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.

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

1 Answer 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

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.