Using Eval to create an Array

D

david.sullivan

I have a string generated by server side code that is like the
following.

((1,'A'),(2,'B')) shortened for brevity.

I was hoping to be able to use javascript's eval function then loop
though the array to do a find an replace.

var tURL = document.getElementById("url").value;
var sArray = Array(eval("(" + document.getElementById
("txtFields").value + ")"));
var iArray;
for (i = 0; i < sArray.length; i++)
{
iArray = sArray;
tURL = tURL.replace(iArray[1], iArray[0]);
}


Am I just going about this all wrong or what?
 
T

Thomas 'PointedEars' Lahn

I have a string generated by server side code that is like the
following.

((1,'A'),(2,'B')) shortened for brevity.
Relevance?

I was hoping to be able to use javascript's eval function then loop
though the array to do a find an replace.

Parse error.
           var tURL = document.getElementById("url").value;

And the value of tURL is ...?
           var sArray = Array(eval("(" + document.getElementById
("txtFields").value + ")"));

And the result of document.getElementById("txtFields").value is ...?
           var iArray;

You can declare that variable within the loop (without changing its scope).
Contrary to popular belief, it will only be declared once.
           for (i = 0; i < sArray.length; i++)

for (var i = 0, len = sArray.length; i < len; i++)

is more efficient. If order does not matter,

for (var i = sArray.length; i--;)

is even faster.
           {
               iArray = sArray;


var iArray = sArray;
               tURL = tURL.replace(iArray[1], iArray[0]);
           }

Am I just going about this all wrong or what?

Maybe. You have neither told what the input nor what the output of this
code is expected to be, and my crystal ball is currently under repair.

<http://jibbering.com/faq/#posting>


PointedEars
 
D

david.sullivan

Fingered it out.
Needed to change the server side code to output a string such as
[[1,'A'],[2,'B']] etc... Instead of ((1,"A"),(2,"B"))

It was supposed to result in an array that I could loop through.
Instead it was resulting into a parser error.
 
D

Dr J R Stockton

In comp.lang.javascript message <41330360-7211-4504-bfe5-4c36eb4a8b0e@x1
6g2000prn.googlegroups.com>, Fri, 16 Jan 2009 13:28:20,
(e-mail address removed) posted:
Fingered it out.
Needed to change the server side code to output a string such as
[[1,'A'],[2,'B']] etc... Instead of ((1,"A"),(2,"B"))

It was supposed to result in an array that I could loop through.
Instead it was resulting into a parser error.

Always remember that Thomas Lahn's responses, when not plainly
obnoxious, seem largely intended to inflate (superfluously) his ego.

You did not **need** to change the server-side code; you could have
changed the string St client-side with
St = St.replace(/\(/g, "[").replace(/\)/g, "]")

Your oroginal code seems to contain superfluities - consider:
X = '[[1,"A"],[2,"B"]]'
eval(X)[1][1] // -> B
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top