Dustine Tolete
Dustine Tolete

Reputation: 471

javascript - add a value on multidimensional array

I am developing a website with bing map. I've almost done it, but i want to have the pin clustering functionalitty. Then i got this code from the internet:

var data = [
        { "latitude": 59.441193, "longitude": 24.729494 },
        { "latitude": 59.432365, "longitude": 24.742992 },
        { "latitude": 59.431602, "longitude": 24.757563 },
        { "latitude": 59.437843, "longitude": 24.765759 },
        { "latitude": 59.439644, "longitude": 24.779041 },
        { "latitude": 59.434776, "longitude": 24.756681 }
    ];

I can get the latitude and longitude of every pushpin on my map, but i don't know how to add the latitude and longitude on this array. Can anyone help me?

Upvotes: 8

Views: 27717

Answers (2)

Siddique Thanikad
Siddique Thanikad

Reputation: 333

Another way is using the spread operator.


let data = [
        { "latitude": 59.441193, "longitude": 24.729494 },
        { "latitude": 59.432365, "longitude": 24.742992 },
        { "latitude": 59.431602, "longitude": 24.757563 },
        { "latitude": 59.437843, "longitude": 24.765759 },
        { "latitude": 59.439644, "longitude": 24.779041 },
        { "latitude": 59.434776, "longitude": 24.756681 }
    ];

data = [...data, {"latitude": lat, "longitude": lng}]

Upvotes: 0

David Byttow
David Byttow

Reputation: 271

data.push({"latitude": lat, "longitude": lng});

Upvotes: 19

Related Questions