-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'
6
  • "it's returning undefined" ? Can you show your exact code ? BTW, that's really how you should access this element. Commented Oct 9, 2013 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. Commented Oct 9, 2013 at 15:37
  • In my real example I use "push" to add the values in a loop Commented Oct 9, 2013 at 15:39
  • possible duplicate of How to return the response from an AJAX call? Commented Oct 9, 2013 at 15:40
  • 1
    @Juhana I don't think this applies here, as there are many calls. Commented Oct 9, 2013 at 15:52

1 Answer 1

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]);
}
4
  • Do we have a canonical "your code is asynchronous" question yet? Commented Oct 9, 2013 at 15:41
  • @Mathletics Yes, there is one. But I think here, as there is a loop and many asynchronous function, there's an opportunity to be more helpful. Commented Oct 9, 2013 at 15:41
  • link to the mentioned canonical? Commented Oct 9, 2013 at 15:51
  • @mako-taco See Juhana's comment. Commented Oct 9, 2013 at 15:53

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.