My 2D array in Javascript always ends up with a length of 0 when I try to push values into it from within a for loop. It remains empty regardless of my attempts.
The issue arises because I am unsure about the number of devices that will be stored in mapList. Despite looking at various examples online, such as codepens and jsfiddles, where similar codes work perfectly fine, I am still puzzled by what's going wrong on my end.
I'm seeking clarification on why the length of my array isn't increasing with each new entry.
Javascript:
var mapList = [];
function getMarkers(){
$.getJSON('getDeviceCoords.php', function(jd) {
for (var i = 0, len = jd.devices.length; i < len; i++) {
mapList.push([jd.devices[i].nickname, jd.devices[i].latitude, jd.devices[i].longitude]);
});
}
console.log(mapList); // [] but expands to show each array entry
console.log(mapList.length); // 0
JSON Response:
{
"status": "OK",
"devices": [
{
"ID": "12:34:56:78:90:FF",
"nickname": "Device 1",
"latitude": "12.3456",
"longitude": "12.3456"
},
{
"ID": "FF:FF:FF:FF:FF:FF",
"nickname": "Device 2",
"latitude": "12.3465",
"longitude": "12.3465"
}
]}
Console Output:
▼[]
►0: (3) ["Device 1", "12.3456", "12.3456"]
►1: (3) ["Device 2", "12.3465", "12.3465"]
length: 2
►__proto__:Array(0)
0