Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    object array and using splits

    I am trying to parse a string so I can put points into google maps api.

    Here is the string i am using:
    Code:
    CountySide Town 66202 KS COUNTRYSIDE,39.025376,-94.766998|Shawnee City 66203 KS SHAWNEE,39.019802,-94.808303
    Here is the javascript I am using:
    Code:
    var a = new Array();
    function parseAddress(addressString)
    {
    	alert(addressString);
    	var prompt = addressString.split("|");
    	for (var i = 0; i < prompt.length; i++) {
    		var details = prompt[i].split(",");
    		var t = new Object();
    		t.name = details[0]
    		t.lat = details[1]
    		t.lng = details[2]
    		a[i] = t;
    	}
    }

    I have found if I do something like this:
    Code:
    var t = new Object();
    t.name = "Shawnee"
    t.lat = 39.019802
    t.lng = -94.708303
    a[0] = t;
    
    var t = new Object();
    t.name = "Overland Park"
    t.lat = 38.992488,
    t.lng = -94.674769
    a[1] = t;
    It will work, can someone tell me why my loop doesnt work?

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,772
    Thanks
    55
    Thanked 518 Times in 515 Posts
    you are better using the push function to push objects onto an array:

    Code:
    <html>
    <head>
    </head>
    <body>
    <script type="text/javascript">
    var a = [];
    
    parseAddress("CountySide Town 66202 KS COUNTRYSIDE,39.025376,-94.766998|Shawnee City 66203 KS SHAWNEE,39.019802,-94.808303")
    
    function parseAddress(addressString){
    
    	var deets = addressString.split("|");
    	for (var i = 0; i < deets.length; i++) {
    		var details = deets[i].split(",");
    		var t = new Object();
    		t.name = details[0]
    		t.lat = details[1]
    		t.lng = details[2]
    		a.push(t);
    	}
    }
    </script>
    
    </body>
    </html>

  • Users who have thanked xelawho for this post:

    homeguard (01-09-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Your loop works just fine. I don't know why you thought it wasn't working.

    Complete web page, using your code virtually untouched (I changed it to create the array in the function and then return the array from the function...minor change), and works perfectly.
    Code:
    <html>
    <head>
    <script type="text/javascript">
    
    function parseAddress(addressString)
    {
    	alert(addressString);
    	var prompt = addressString.split("|");
    	var a = new Array();
    	for (var i = 0; i < prompt.length; i++) {
    		var details = prompt[i].split(",");
    		var t = new Object();
    		t.name = details[0]
    		t.lat = details[1]
    		t.lng = details[2]
    		a[i] = t;
    	}
            return a;
    }
    
    var s = "CountySide Town 66202 KS COUNTRYSIDE,39.025376,-94.766998|Shawnee City 66203 KS SHAWNEE,39.019802,-94.808303";
    
    var locations = parseAddress(s);
    var show = "";
    for ( var n = 0; n < locations.length; ++n )
    {
        var loc = locations[n];
        show += "name: " + loc.name + "\nlat: " + loc.lat + "\nlng: " + loc.lng + "\n--------------\n";
    }
    alert(show);
    </script>
    </head>
    <body>
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    AHHH!!! But that was in FireFox!

    I'll bet it fails in MSIE! Or maybe Chrome.

    And that's because you used the name prompt which is also the function used to prompt users for input!

    Try just changing prompt to some other name. Even using Prompt should work, since JS is case sensitive.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •