Reputation: 2527
I would like to read geographic data (logitude/latitude) from two sources into one Javascript array. I created an javascript object with an array to hold this data
This is my object definition:
var GeoObject = {
"info": [ ]
};
When reading the two sources of data, if the key RecordId already exists in an array, then append new array elements (lat&lon) to existing GeoObject, otherwise add a new array record.
for instance, if the RecordId 99999 does not already exist then add array (like an SQL add)
GeoObject.info.push(
{ "RecordId": "99999" , "Bing_long": -75.0000, "Bing_lat": 41.0000 } )
if record 99999 already exists, then append new data to existing array (like an SQL update).
GeoObject.info.update???(
{ "RecordId": "99999" , "Google_long": -75.0001, "Google_lat": 41.0001 } )
When the application is finished, each array in the object should have five array elements including the RecordId. Examples:
[ "RecordId": "88888" , "Bing_long": -74.0000, "Bing_lat": 40.0001, "Google_long": -74.0001, "Bing_long": -70.0001 ]
[ "RecordId": "99999" , "Bing_long": -75.0000, "Bing_lat": 41.0001, "Google_long": -75.0001, "Bing_long": -75.0001 ]
I hope that I am clear. This pretty new and a bit complex for me.
Maybe the object definition is not ideal for this case.
Upvotes: 1
Views: 1325
Reputation: 63812
I'd make an object of objects.
var GeoObject = {
// empty
}
function addRecords(idAsAString, records) {
if (GeoObject[idAsAString] === undefined) {
GeoObject[idAsAString] = records;
} else {
for (var i in records) {
GeoObject[idAsAString][i] = records[i];
}
}
}
// makes a new
addRecords('9990', { "Bing_long": -75.0000, "Bing_lat": 41.0000 });
//updates:
addRecords('9990', { "Google_long": -75.0001, "Google_lat": 41.0001 });
This gives you an object that looks like this:
GeoObject = { '9990' : { "Bing_long": -75.0000,
"Bing_lat": 41.0000,
"Google_long": -75.0001,
"Google_lat": 41.0001 }
}
With a second record it would look like this:
GeoObject = { '9990' : { "Bing_long": -75.0000,
"Bing_lat": 41.0000,
"Google_long": -75.0001,
"Google_lat": 41.0001 },
'1212' : { "Bing_long": -35.0000,
"Bing_lat": 21.0000 }
}
Upvotes: 1