问题描述
我有这个功能
// 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;
}
}
除了使用 addMarkers() 添加标记外,我还想将经纬度和时间戳存储在一个对象中.
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 } }
或
{ strUserName : { timestamp : point.timestamp , LatLng : { lat : point.lat, lng : point.lng } }, ..
如何创建这个对象?
更新:
感谢您的回复.我已经尝试了以下..
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);
}
查看控制台截图
用户名无效,我没有为每个时间戳获取一个项目,它只是覆盖了一个条目?
the username has not worked and im not getting an item for each timestamp, its just overwriting the one entry?
推荐答案
与您所做的几乎一模一样:
Pretty much exactly like you did:
var obj = { strUserName : { timestamp : point.timestamp , LatLng : point.LatLng }, strUserName : { timestamp : point.timestamp , LatLng : point.LatLng } };
或更易读:
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.
举一些简单的例子:
// 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:
用户名无效,我没有为每个时间戳获取一个项目,它只是覆盖了一个条目?
当然是这样,您在每个循环中都覆盖了 history
,而不会将之前的副本存储在任何地方.例如,您可以将它们存储在一个数组中:
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);
}
这篇关于创建javascript对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!