1

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

0

2 Answers 2

1

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));
3
  • Not "always", the index named properties that correspond to each character position were recently introduced 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 Commented Oct 20, 2010 at 20:23
  • @CMS you're right, I forgot about charAt. I'll update the answer. Commented Oct 20, 2010 at 20:24
  • Thanks, array.push syntax was what I needed! (with the data.getValue) Commented Oct 20, 2010 at 21:28
1
mls_link.push(data.getValue(i,1))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.