How to remove square brackets in a Javascript array?

I have an array called value , and when I console.log(value) , I get about 30 lines of code with the following [6.443663, 3.419248]

Numbers change as they are latitudes and longitudes. But I need to somehow get rid of the square brackets around each one.

I tried var replaced = value.toString().replace(/\[.*\]/g,''); but this changed the above example to 6.443407,3.419035000000008 and my code failed to execute.

In other words, I need help to get rid of the square brackets in the array in order to display the points on my map.

My array consists of the following code, because each latitude and longitude value is stored inside onClick functions within a links. So the member here has kindly provided the code below to get my values ​​in an array, but now I'm struggling to get the values ​​without any brackets destroying it.

 var obj = {}; $('.choice-location-inner a').each(function(){ var $this = $(this); var text = $this.attr('onClick').replace('findLocation(\'', '').replace('\');return false;', ''); var array = text.split(',') obj[this.id]= [parseFloat(array[0]), parseFloat(array[1])]; }); 

The array is trying to display markers on my google map using the following code

 $.each(obj , function(index, value){ geocoder = new google.maps.Geocoder(); geocoder.geocode( { 'address': value}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); marker = new google.maps.Marker({ map: map, icon: image, position: results[0].geometry.location }); } else if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { wait = true; setTimeout("wait = false", 1000); } else { alert("Geocode was not successful for the following reason: " + status); } }); }); 
+4
source share
3 answers

If I read it incorrectly, and the element is an object, not a string with the brackets contained, you can simply access it using your subset of the array

 lat = obj[this.id][0] lng = obj[this.id][1] 

perhaps?

+7
source

converts a string into an array into text.

 var value = [[1234213, 1234231], [1234213, 1234231]]; var dato = value[0]; console.log(dato.toString()); 
+6
source

It looks like you want the string representation of the array to be comma separated. In this case, you can do this:

 var commasep = value.join(","); 
+2
source

All Articles