Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]

The 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 failed my code.

In other words I need help getting rid of square brackets in an array in order to plot points on my map.

My array is made up from the following code because each latitude and longitude value is held within onClick functions within a links. So a member on here kindly supplied the below code to get my values into an array but now I'm struggling to get the values out without any brackets ruining 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 plot 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);
        }
    });
});
like image 693
ngplayground Avatar asked Apr 18 '13 15:04

ngplayground


People also ask

How do you remove square brackets in JSON array?

To remove the square brackets that surround the JSON output of the FOR JSON clause by default, specify the WITHOUT_ARRAY_WRAPPER option. Use this option with a single-row result to generate a single JSON object as output instead of an array with a single element.

How do you remove square brackets?

Brackets can be removed from a string in Javascript by using a regular expression in combination with the . replace() method.

How do you remove an element from an array in JavaScript?

Array elements can be deleted using the JavaScript operator delete . Using delete leaves undefined holes in the array. Use pop() or shift() instead.


3 Answers

transforms the line to array to text.

var value = [[1234213, 1234231], [1234213, 1234231]];
var dato = value[0];
console.log(dato.toString());
like image 177
AURIGADL Avatar answered Oct 17 '22 20:10

AURIGADL


Unless I've misread and the item is an object rather than a string with brackets contained, the you could just access it by its sub array poition

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

perhaps?

like image 10
Gga Avatar answered Oct 17 '22 20:10

Gga


It looks like you want the comma separated string representation of the array. If that's the case, you can do this:

var commasep = value.join(",");
like image 6
Adriano Carneiro Avatar answered Oct 17 '22 20:10

Adriano Carneiro