-2

My array looks like this:

var array = [
  ['fred', 123, 424],
  ['johnny', 111, 222]
]

etc...

I simply would like to select the second value in the first array (123, i.e. the value in the "fred" array) like so:

array[0][1];

But it's returning undefined. When I do console.log(array), I get the following:

Array[1], Array[1], Array[1], Array[1]]
  0: Array[3]
    0: "fred"
    1: 123
    2: 424

etc...

How do I get the value of that second item using the syntax described above?

Thanks!

Here is the full code:

var addresses = [
    ['Bondi Beach Australia'],
    ['Coogee Beach Australia'],
    ['Cronulla Beach Australia'],
    ['Manly Beach Australia']
];

for (i = 0; i < addresses.length; i++) {
    (function(address){

        geocoder.geocode( { 'address': addresses[i][0]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                address.push(results[0].geometry.location.lb);
                address.push(results[0].geometry.location.mb);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });

    })(addresses[i]);
}

console.log(addresses[0][1]); // Returns 'undefined'
HandiworkNYC.com
  • 10,914
  • 25
  • 92
  • 154
  • *"it's returning undefined"* ? Can you show your exact code ? BTW, that's really how you should access this element. – Denys Séguret Oct 09 '13 at 15:35
  • 3
    The "example" array and the "real" array have different structures (former is 2-deep, latter is 3-deep). Why even give an example if it's misleading? `addresses[0][0][1]` would work. – Jon Oct 09 '13 at 15:37
  • In my real example I use "push" to add the values in a loop – HandiworkNYC.com Oct 09 '13 at 15:39
  • possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – JJJ Oct 09 '13 at 15:40
  • 1
    @Juhana I don't think this applies here, as there are many calls. – Denys Séguret Oct 09 '13 at 15:52
  • @j-man86 If the answer I gave doesn't work or apply to you, can you precise why ? – Denys Séguret Oct 10 '13 at 07:58

1 Answers1

3

Your problem is that geocode is an asynchronous function and you log before it finishes.

As you launch your queries in a loop, you probably want to wait for all of them to finish before you do the other operations (among them your log).

Here's a solution :

var n = addresses.length;
function check() {
   if (--n===0) {
      // everything's finished
      console.log(addresses[0][1]);
   }
}
for (i = 0; i < addresses.length; i++) {
    (function(address){
        geocoder.geocode( { 'address': addresses[i][0]}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                address.push(results[0].geometry.location.lb);
                address.push(results[0].geometry.location.mb);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
            check();
        });
    })(addresses[i]);
}
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758