Reputation: 3620
I have this function
// add history paths and save data
function AddPath( strTag, strUserName, arrayLatLngPoints, pathColour) {
for (var i = 0; i < arrayLatLngPoints.length; i++)
{
var point = arrayLatLngPoints[i];
var date = new Date( parseInt( point.timestamp));
addMarkers(point.timestamp, point.lat, point.lng, point.timestamp, strUserName, pathColour, date.toString());
date = null;
}
}
as well as adding the markers with addMarkers() i want to store the lat, long and timestamp in an object.
I was thinking the best way to store would be like this
{ strUserName : { timestamp : point.timestamp , LatLng : point.LatLng }, strUserName : { timestamp : point.timestamp , LatLng : point.LatLng } }
or
{ strUserName : { timestamp : point.timestamp , LatLng : { lat : point.lat, lng : point.lng } }, ..
How can i create this object?
UPDATE:
Thanks for the replies. I have tried the following..
// add history paths and save data
function AddPath( strTag, strUserName, arrayLatLngPoints, pathColour) {
for (var i = 0; i < arrayLatLngPoints.length; i++)
{
var point = arrayLatLngPoints[i];
var pos = new google.maps.LatLng(point.lat, point.lng);
var history = {
strUserName : {
timestamp : point.timestamp ,
LatLng : pos
}
};
var date = new Date( parseInt( point.timestamp));
addMarkers(point.timestamp, point.lat, point.lng, point.timestamp, strUserName, pathColour, date.toString());
date = null;
}
console.log(history);
}
see screenshot of console
the username has not worked and im not getting an item for each timestamp, its just overwriting the one entry?
Upvotes: 0
Views: 1307
Reputation: 1073968
How can i create this object?
Pretty much exactly like you did:
var obj = { strUserName : { timestamp : point.timestamp , LatLng : point.LatLng }, strUserName : { timestamp : point.timestamp , LatLng : point.LatLng } };
Or more readably:
var obj = {
strUserName : {
timestamp : point.timestamp ,
LatLng : point.LatLng
},
strUserName : {
timestamp : point.timestamp ,
LatLng : point.LatLng
}
};
That's an object initializer. It creates a new object with the given properties (actually, three new objects) and returns a reference to the outermost of them.
Taking some simpler examples:
// Create a blank object (an object with no properties of its own):
var a = {};
// Create an object with a `name` property with the value "Fred"
var b = {name: "Fred"};
// Create an object with a `foo` property, which is *another* freshly-created
// object with a `name` property with the value "Barney"
var c = {
foo: {
name: "Barney"
}
};
Re your updated question:
the username has not worked and im not getting an item for each timestamp, its just overwriting the one entry?
Of course it is, you're overwriting history
on each loop, without storing the earlier copy anywhere. For instance, you can store them in an array:
// add history paths and save data
function AddPath( strTag, strUserName, arrayLatLngPoints, pathColour) {
var historyArray = [];
for (var i = 0; i < arrayLatLngPoints.length; i++)
{
var point = arrayLatLngPoints[i];
var pos = new google.maps.LatLng(point.lat, point.lng);
historyArray[i] = {
strUserName : {
timestamp : point.timestamp ,
LatLng : pos
}
};
var date = new Date( parseInt( point.timestamp));
addMarkers(point.timestamp, point.lat, point.lng, point.timestamp, strUserName, pathColour, date.toString());
date = null;
}
console.log(historyArray);
}
Upvotes: 3
Reputation: 4549
var obj = {strUserName: {timestamp: point.timestamp, lat: point.lat, long: point.long}}
Upvotes: 0