create javascript object

风格不统一 提交于 2020-01-05 20:01:46

问题


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?


回答1:


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);
}



回答2:


var obj = {strUserName: {timestamp: point.timestamp, lat: point.lat, long: point.long}}


来源:https://stackoverflow.com/questions/11308981/create-javascript-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!