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

Recommended Answers

All 3 Replies

Try asking the question on a javascript forum for better answers.

Claire,

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

String.split() forms an Array automatically.

Airshow

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("|");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.