7

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);
        }
    });
});
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • What *exactly* is `value`? An array, a string? `console.log(typeof value)` please. – gen_Eric Apr 18 '13 at 15:43
  • Do you mean the lat/lng values are stored as a string in the array, such as array[0] = "[23.021321,18.213123]"? – Gga Apr 18 '13 at 15:44
  • 1
    If `value` is array, then `value.toString()` is the same as `value.join(',')`. – gen_Eric Apr 18 '13 at 15:44
  • @RocketHazmat `console.log(typeof value)` returns as `Object` – ngplayground Apr 18 '13 at 15:44
  • @DonaldSutherland: So, what *exactly* are you trying to do with the array? Display it? `console.log` it? What? (remember, your array is multi-dimentional) – gen_Eric Apr 18 '13 at 15:47
  • @RocketHazmat I have updated the code to show you how I'm trying to get the array to work. I'm trying to plot each array item onto my Google Map – ngplayground Apr 18 '13 at 15:49
  • Can you show the exact output from firebug for console.log(value) – Gga Apr 18 '13 at 15:52
  • @DonaldSutherland: Ok, so what's the issue? Does that not work? Have you tried just doing `{ 'address': value.join(',') }`? – gen_Eric Apr 18 '13 at 15:52
  • @DonaldSutherland: Since you are sending coordinates and not an address, have you tried using `new google.maps.LatLng` and `geocoder.geocode({'latLng': latlng}` (http://stackoverflow.com/a/2993614)? – gen_Eric Apr 18 '13 at 15:54
  • When you `console.log` an array, the `[]` are shown to tell you it's an array. It's *not* logging a string. If you want a string, you need to convert it to one. `value.toString() is the same as value.join(',')`. – gen_Eric Apr 18 '13 at 15:56
  • @RocketHazmat When I used `value.toString()` the following `[6.443663, 3.419248]` gets changed to `6.443407,3.419035000000008` – ngplayground Apr 18 '13 at 15:58
  • @DonaldSutherland: Yes, yes it does. That's because `value.toString()` is the same as `value.join(',')`, which return strings. When `console.log` is called on a string, they are displayed as-is, when it's called on an array, `[]` is *added* to show that it's an array. (Also, you're getting `3.419035000000008` because floating-point numbers are weird.) Again, what is not working about this code? – gen_Eric Apr 18 '13 at 16:02
  • @DonaldSutherland: Yea, that's fine. It's a string now. Does `{ 'address': value.toString() }` not work? – gen_Eric Apr 18 '13 at 16:08

5 Answers5

12

transforms the line to array to text.

var value = [[1234213, 1234231], [1234213, 1234231]];
var dato = value[0];
console.log(dato.toString());
AURIGADL
  • 1,832
  • 2
  • 13
  • 15
11

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?

Gga
  • 4,311
  • 14
  • 39
  • 74
6

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(",");
Adriano Carneiro
  • 57,693
  • 12
  • 90
  • 123
0
`${arr.map(({ id }) => id)}`

Maybe this way a lot simple other than using inbuilt functions!

Jacksparrow
  • 15
  • 1
  • 4
0

If you need a clean input you can write this.

var justTheDataWithoutSeparation = data.split('');

of course, if you want to separate with commas or something else, you need to add

vat dataSeparatedWithSomething = data.split('-');

You need to be careful because doing this method can cause your data to set like string value, and may affect your output, you need to convert that before.

Hectorromerodev
  • 405
  • 5
  • 9