/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Create a new var from a string

Hi,

I have an annoying problem that I have no idea where to start. What I want to do is create a var that is ‘discouverd’ from a loop that creates a string. I will give you my working example.

I have 2 arrays that loop though to create a string

[code]var days = new Array()
days[0] = “mon”
days[1] = “tues”
days[2] = “wed”
days[3] = “thurs”
days[4] = “fri”

var varname = new Array()
varname[0] = “date”
varname[1] = “who”
varname[2] = “where”
varname[3] = “time”[/code]

There are my 2 arrays, easy enough, now I want to create a string from these, so I have this;

[code]for(i=1;i<=weeks1;i++)
{
var varNew = i+days[0]+varname[0]

document.write(varNew)

}[/code]

This also works fine, the trouble is, I dont want to assign var varNew the string, I want the string to be a var it self. So 1mondate is the first string created, I want this to become a varible so I get var 1mondate and not varNew = 1mondate

I hope you understand, I am crap at explaning things ?.

So….create a var using a string created from a loop.

Thanks for your help
Chris

to post a comment
JavaScript

9 Comments(s)

Copy linkTweet thisAlerts:
@CharlesSep 01.2006 — &lt;script type="text/javascript"&gt;
for (var i = 0; i &lt; 10; i++) {
self['s' + i] = 'foo' + i
}
alert (s5)
&lt;/script&gt;
Copy linkTweet thisAlerts:
@kicken18authorSep 01.2006 — Sorry but that has slightly confused me :S
Copy linkTweet thisAlerts:
@CharlesSep 01.2006 — No doubt. But you'll note that it works and if you use it a while it will make sense.

In JavaScript you cannot make global variables. You can't even make variables. Variables are really just properties of some object and global variables are objects of the window object. "self" is an easier to type synonym for "window". And in JavaScript objects, methods and properties can all be addressed as associative arrays.
Copy linkTweet thisAlerts:
@kicken18authorSep 01.2006 — Ok I ran it and it makes more sense...and I can output that, but can I not create a var from what is created?

ok so hang on a sec, I have created a var of s5 but the actuel content of that, is foo5. So I can create v1 v2 v3 etc but the content of those vars will be what I want...
Copy linkTweet thisAlerts:
@CharlesSep 01.2006 — What do you mean by that? You are creating vars or what passes for vars in JavaScript.
Copy linkTweet thisAlerts:
@kicken18authorSep 01.2006 — I edited the above post to add another line
Copy linkTweet thisAlerts:
@WhoknowSep 01.2006 — is this what you want:

<script type="text/javascript">

for (var i = 0; i < 10; i++) {

self[i+days[i]+varname[i]] = i+days[i]+varname[i];

}

</script>
[/QUOTE]
Copy linkTweet thisAlerts:
@kicken18authorSep 01.2006 — Ahh that works well, Now i just need to break it up so I get 1monwho 1monwhere.....1tueswho 1tueswhere....4wedwho 4turswhen ETC ETC....sure ill post again when I get stuck lol

Thanks for you help
Copy linkTweet thisAlerts:
@kicken18authorSep 01.2006 — ah no, imbedded for lopps inbedded in a for loop is as simle as adding another loop inside each other. ahh a nice big outputted list...now to try and make a working model.

Thanks

Chris
×

Success!

Help @kicken18 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...