0

I got ten javascript variables named street1, street2, street3 ... street10.

I have to do a loop by putting into these variables. so I did:

var data;
for (var i=1;i<=10;i++)
{
data = data + 'this is street '+street+i+'\n';
}   

console log prints "street" does not exist...which is right because my variable is named street1. there's a way I can "mix" variables and index?

6
  • 6
    Why don't you use an array? Commented Aug 20, 2014 at 11:07
  • so there isn't a solution? if so i'll convert in an array. Commented Aug 20, 2014 at 11:13
  • 1
    Not to be rude, but arrays should really be the first thing to try when doing things like this, it's what they were made for. Commented Aug 20, 2014 at 11:24
  • yeah, i know, i wantedd to know if there is such method...nevermind,for now on, always using arrays! Commented Aug 20, 2014 at 11:33
  • @Marco: There is such a method - as a few answers have highlighted. Array's are the more common way of handling it, but it's still perfectly dooable. Commented Aug 20, 2014 at 11:33

4 Answers 4

3

You really should use an array for this:

var street = [];
street.push("Fake St");
for(var i=0; i<street.length; i++) {
    data += "this is street: " + street[i] + "\n";
}

But you can achieve your original idea using bracket notation:

var data;
for (var i=1;i<=10;i++)
{
data = data + 'this is street '+ this["street"+i] +'\n';
}  
Sign up to request clarification or add additional context in comments.

Comments

3

It would make more sense to store this item in an array:

var items = ["a", "b", "c"];
for (var i=0;i<3;i++)
{
   Console.log(items[i]);
}

Alternatively you can use square bracket notation. Accessing Street1 will depend on where it lives, assuming you've not scoped anything the following will get you the value, depending on where street1 is defined:

this['street' + i]
window['street' + i]

3 Comments

using square bracket returns same error in console log...if there isn't a solution i'll have to use an array..
@Marco: If you could set up a quick JSFiddle I'll fix it for you - it's most likely down to where you've defined street1 which isnt' clear from your question.
@Marco It depends where street1 is defined. You may need to use window, or define your variable explicitly
2

It is possible to use eval to do this, but it's slow, dangerous, and just all-around bad practice, so i'm not going to show you.

What you can do instead is to use an array.

var streets = ['street1','street2','etc.','street10'];

Then, you can loop through the array and get the values:

var data;
for (var i=0;i<10;i++)
{
data = data + 'this is street '+streets[i]+'\n';
}   

1 Comment

yes, in the end i have to use an array. i know eval vould be too risky. thanks.
1

The generally accepted way to do this is using arrays. Your code would look something like this:

var streets = ["Park street", "First street", "Pine street", "Rainbow road", "Yellow brick road", "View street", "Ninth street", "Cedar street", "Lake court", "Hill street"];

for(var i = 0; i < streets.length; i++) {
    data += "this is street " + streets[i] + "\n";
}

The key improvement is that it will auto-adjust to however many streets are in the array due to the i < streets.length part of the for loop.

You could do it with eval(), but besides it being inefficient and more trouble than it's worth, that can be dangerous.

1 Comment

thanks. I'll use an array instead. your code is minimal and elegant.

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.