简体   繁体   中英

How to insert dynamic values in Array of object

I have an array of object with static values. This array of the object is used in EON Map for real-time tracking. I want to add values in this array dynamically I mean using a loop from the database.

Please suggest me how can I put my dynamic values in this array?

var torchys = [
    {
        latlng: [30.370375, -97.756138]
    },
    {
        latlng: [30.323118, -97.739144]
    },
    {
        latlng: [30.302816, -97.699490]
    },
    {
        latlng: [30.293479, -97.742405]
    },
    {
        latlng: [30.250337, -97.754593]
    },
    {
        latlng: [30.236689, -97.762730]
    }
];

You're probably looking for the push method. For example:

torchys.push({latlng: [30.370375, -97.756138]})

@brk actually i have to show real time Taxies on map. i have lat longs but i don't know how to put my dynamic lat long in this format. the given array working fine.

Using torchys.push(...) should be fine in your case. I'm not sure what you exactly mean by using loop from database .

You can use this method to push the dynamic latlng value into array.

const newArray = [];
for(var i=0; i< 10; i++){
   newArray.push({latlng: [i, i+1]});
}
console.log(newArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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