views:

48

answers:

2

Ok basic JS 101 I know, but can't find the answer...

For the below code I have declared var mls_link = []; In globals

The data.getValue yields a string item (http addresses) When I step through the code the string is an array rather than each "item" being a array entry.. ie mls_link [0] is h (the beginning of the http address) I want each element to be addressable as an http address so when I ask for mls_link[0] I get 'http://someaddress.com'

for ( var i = 0; i < data.getNumberOfRows(); i++ )
        mls_link+=(data.getValue(i,1)); 

Thanks

+1  A: 
mls_link.push(data.getValue(i,1))
Edgar Bonet
+1  A: 

In many implementations of Javascript, strings can be indexed like an array (however, as CMS correctly pointed out in the comments, the correct cross-browser way to do this, however, is by using String.charAt). ie:

var s = "hello world";
alert(s[6]); // "w"

If you want to add a value to an array, use Array.push:

mls_link.push(data.getValue(i, 1));
Daniel Vandersluis
Not *"always"*, the *index named* properties that correspond to each character position were [recently introduced](http://ecma262-5.com/els5_html.htm#Section_15.5.5) in the ECMAScript 5th Edition Spec., even though this was supported as a non-standard ES3 extension by some ES3-based implementations, it doesn't work in some browsers (IE8 in compat. mode, IE7, etc, are good examples). That's why the `String.prototype.charAt` method exist, `s.charAt(6); // w`
CMS
@CMS you're right, I forgot about charAt. I'll update the answer.
Daniel Vandersluis
Thanks, array.push syntax was what I needed! (with the data.getValue)
dartdog