0

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"]
3
  • What are lat and lng? Commented Jul 4, 2019 at 0:01
  • @JackBashford just updated with lat and lng console log Commented Jul 4, 2019 at 0:02
  • This post seems to be lacking alot of details. I am guessing that there is a loop to collect a number of coords and put them in a new array of coords as arrays. Please clarify what it is you are trying to accomplish. Do not ask X when you want to know Y. Commented Jul 4, 2019 at 3:35

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

thing is I believe the json is broken, if I do that I get Array(2), "40.7197406, 8.563512299999957", "40.7272074, 8.575266499999998", Array(2)
0

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

4 Comments

they will be separated tho, there after I won't be able to use var pin = coords[i][0].split(',');
Then just use var comboCoords = lat+","+lng; then push it to the array arr.push(comboCoords); You can verify the result using alert(JSON.stringify(arr));
is it not what I did in the question?
For some reason you parsed your data. I have reproduced an working example: jsfiddle.net/vk8216da/1
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.