Reputation: 10571
I do:
var lat = data[i]["usp-custom-90"]["usp-custom-19"];
var lng = data[i]["usp-custom-90"]["usp-custom-20"];
var comboCoords = lat+","+lng;
But comboCoords
is a string while I need it as an array and push that
I tried:
coords.push([lat, lng]);
But since I need to split them because I do:
for (var i = 0; i < coords.length; i++) {
var pin = coords[i][0].split(',');
var latLng = new google.maps.LatLng(pin[0], pin[1]);
I get
TypeError: coords[i][0].split is not a function
I tried
var comboCoords = JSON.parse(lat+","+lng);
coords.push(comboCoords);
But I get
Unexpected token , in JSON at position 6
if I console.log
lat
and lng
I get:
["39.213"]0: "39.213"length: 1__proto__: Array(0)
(index):575 ["9.126"]
Upvotes: 0
Views: 97
Reputation: 446
Try this
var string1="Content1";
var string2="Content2";
var array = string1+string2.split(" ");
var output =""
for(i=0;i<array.length;i++){
output += array[i];
}
console.log(output);
Upvotes: 0
Reputation: 30893
It feels like you want something like:
coords = [
[
39.213,
9.126
],
[
39.225,
9.135
]
];
Essentially a matrix, an Array of Arrays that contain 2 elements each.
I would suggest an Array of Objects:
coords = [
{
lat: 39.213,
lng: 9.126
},
{
lat: 39.225,
lng: 9.135
}
];
While you're iterating your data
, you can populate this into the array.
var coords = [];
for(var i = 0; i < data.length; i++){
coords.push({
lat: data[i]["usp-custom-90"]["usp-custom-19"],
lng: data[i]["usp-custom-90"]["usp-custom-20"]
});
}
You will now have an array of coords
that contains objects. You can access it like:
var data = [{
"usp-custom-90": {
"usp-custom-19": 39.213,
"usp-custom-20": 9.126
}
}, {
"usp-custom-90": {
"usp-custom-19": 39.225,
"usp-custom-20": 9.135
}
}];
var coords = [];
for (var i = 0; i < data.length; i++) {
coords.push({
lat: data[i]["usp-custom-90"]["usp-custom-19"],
lng: data[i]["usp-custom-90"]["usp-custom-20"]
});
}
console.log(coords[0].lat + "," + coords[0].lng);
console.log(coords[1]['lat'] + "," + coords[1]['lng']);
Hope that helps.
Upvotes: 1
Reputation: 3
You don't need the [] in the push statement.
var arr = [];
var lat = data[i]["usp-custom-90"]["usp-custom-19"];
var lng = data[i]["usp-custom-90"]["usp-custom-20"];
var comboCoords = lat+","+lng;
arr.push(comboCoords);
Upvotes: 0
Reputation: 44107
Currently, lat
and lng
are arrays. You can't split
an array - spread into the pushed array:
coords.push([...lat, ...lng]);
Or just use index access:
coords.push([lat[0], lng[0]]);
Upvotes: 0