1

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.

1
  • 1
    Perhaps, make it in the format: [info: {recordId1 => {data1}, recordId2 => {data2}}], "push or update" is then just info[recId] = blahblah. To keep it the current format will require a secondary DS or iterating each time (to filter or to mutate), which is not necessarily bad.
    – user166390
    Commented Dec 28, 2011 at 3:23

1 Answer 1

1

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 }
}
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.