Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27154

Re: javascript loop problem

From "Christoph M. Becker" <cmbecker69@arcor.de>
Newsgroups comp.lang.javascript
Subject Re: javascript loop problem
Date 2015-04-01 14:42 +0200
Organization solani.org
Message-ID <mfgp3r$ir$1@solani.org> (permalink)
References <8f8b8bbd-fed1-4871-b3e4-e0b96abd3692@googlegroups.com>

Show all headers | View raw


chrisb@ctlsoft.com wrote:

> little script which adds map markers to the appropriate locations with a label.
> The array 'myaddresses' contain the locations to mark on the map and the numbers to put on the labels.
> Works fine for the location bit, but the labels all show the last count (35).
> 
> So the line labelContent: myaddresses[x][1] always seems to return the final array element for some reason.

When this expression is evaluated, x === myaddresses.length-1.

> <script type='text/javascript'>
> $(window).load(function(){
> $(document).ready(function () {

That seems clumsy at best.  You'll want to skip registering an event
listener for window's load event, here.

>     var myOptions = {
>         zoom: 6,
>         center: new google.maps.LatLng(-42.397, 172.644),
>         mapTypeId: 'terrain'
>     };
>     var map = new google.maps.Map($('#map_canvas')[0], myOptions);
> 
>     var myaddresses = [['Cambridge', 100], ['Auckland',20], ['Wellington',1], ['Taupo',35]];
> 
>     for (var x = 0; x < myaddresses.length-1; x++) {

Don't use a for loop here, but rather $.each().

-- 
Christoph M. Becker

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

javascript loop problem chrisb@ctlsoft.com - 2015-03-31 19:05 -0700
  Re: javascript loop problem Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2015-04-01 10:49 +0800
  Re: javascript loop problem "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-01 14:42 +0200
  Re: javascript loop problem Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-01 14:43 +0000

csiph-web