I am currently working on creating an object using underscore and backbone. I have an array of objects, each containing a nested object with different sets of data.
Within the array, data[0] holds the name of a location while data[2] contains the coordinates for that location.
data: Array[3] 0: Objectcol_id: "4" data: "W Hotel Union
Square" __proto__: Object1: Object2: Objectcol_id: "13"data: "40.736638, -73.988396"
To extract the coordinates and add them to a new array, I am using the following function:
var newarr = _.map(rawData, function (a) { return [a.records[0].data[2].data] });
I then split the array of coordinates and create key-value pairs in a new object to set the latitude and longitude.
var newnewarr = [];
for (i = 0; i < newarr.length; i++) {
newnewarr[i] = _.map(newarr[i][0].split(","), function(s){ return parseFloat(s);
});
}
function longlat(lat, long) {
this.Latitude = lat; this.Longitude = long;
};
var coordinates = [];
for (i = 0; i < newnewarr.length; i++) {
coordinates[i] = new longlat(newnewarr[i][0], newnewarr[i][1]);
}
Now, my objective is to create an array of objects in the format below:
newarr = [{
latitude: 40.736638,
longitude: -73.988396,
title: "W hotel Union Square"
},
{
latitude: 40.736638,
longitude: -73.988396,
title: "Union Square Park"
}];
I am struggling to achieve this using my existing code. I attempted iterating over the data object but encountered difficulties. Specifically, I am looking to iterate over data[0] and data[2], retrieve those values, and then populate them into an object as described above. Any suggestions or guidance on how to accomplish this would be greatly appreciated!
var newarr = _.map(rawData, function (a) { return [a.records[0].data[2].data] });