0

Hi there

I am new here and wanted to ask question
I have this simple code to put elements into an array

<html>
<body>

<script type="text/javascript">
         var numb =2;            
        var tempstr="-26.18101|-26.161011|";    
        var latstr=new Array(tempstr.split("|")); 
        
          document.write((latstr[0]) + "<br />");
          document.write((latstr[1]) + "<br />");
          document.write((latstr[2]) + "<br />");

        
</script>

</body>
</html>

What it is returning is
-26.18101,-26.161011,
undefined
undefined

Where as I would want and expect
-26.18101
-26.161011
undefined

Any ideas please!!!
Thanks

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Taywin
0

Claire,

var latstr = tempstr.split('|');

String.split() forms an Array automatically.

Airshow

Edited by Airshow: n/a

0
var tempstr="-26.18101|-26.161011|";

// create an array within an array
// [["-26.18101", "-26.161011", ""]]
// as a result, -26.18101, -26.161011, undefined, undefined
var latstr=new Array(tempstr.split("|"));

// create an array
// ["-26.18101", "-26.161011", ""]
// as a result, -26.18101, -26.161011, undefined
var latstr2=tempstr.split("|");

Edited by Taywin: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.