I am trying to create an object of locations for placing points on a map.
My code is:
var Locs = [{}];
var title = '', content = '';
for (i = 0; i < locations.length; i++) {
content = '<div id="bodyContent">' +
'<p><b>' + locations[i][0] + '</b><br />' +
locations[i][4] +
'</div>';
Locs[i]['lat'] = locations[i][1];
Locs[i]['lon'] = locations[i][2];
Locs[i]['zoom'] = zoom;
Locs[i]['title'] = locations[i][0];
Locs[i]['html'] = content;
}
I keep getting an error: TypeError: Locs[i] is undefined
If I replace i with 0 it works for displaying one point on the map.
What I need to output is:
var Locs = [
{
lat: 45.9,
lon: 10.9,
zoom: 3,
title: 'Title A1',
html: '<h3>Content A1</h3>'
},
{
lat: 44.8,
lon: 1.7,
zoom: 3,
title: 'Title B1'
html: '<h3>Content B1</h3>'
},
{
lat: 51.5,
lon: -1.1,
zoom: 3,
title: 'Title C1',
html: '<h3>Content C1</h3>'
}
];
So I am wondering if someone can explain to me what I don't understand about creating an object dynamically?
Locsalready defined as a method or something, what happens if you change the name tolocation_arrayLocs = []then laterLocs.push({ lat: 1, lon: 2, etc })var Locs = [];and then initialize each object, on each iteration :Locs[i] = {};Locs[i] = {};to define a new object.var Locs = []also,