I want to create a variable with the dynamic name with javascript

Asked

Viewed 314 times

-5

To illustrate better, I have one for, and I want for each time I rotate the loop, to create a variable with the NOME plus the for ex index:

for(i=0;i<5;i++){
 var "variavel+i" = i
 console.log("variavel+i")
}

In this case the loop would create the variables as follows:

var variavel0

var variavel1

var variavel2

And so on. In addition to printing on the console the values 0,1,2 ...

  • 4

    You can create an array and insert the values into the array, right? That’s exactly what they’re for.

  • 4

    I think you’re about to do some unnecessary snooping. A way to make a global variable would be with window["variavel" + i], but it’s completely unnecessary.

  • variavel[i] = i; Simple as that

  • 12

    If your problem goes well beyond that it would be nice you explain it. So we can offer useful solutions.

  • 2

    And why can’t you use array if this is the appropriate solution?

  • @Unfair Maniero - AP was very clear in his question of how to create a series of variables with different names without having to declare one by one (dynamically) - he did not ask about arrays

  • @Sorack the AP was very clear on what he needs

  • 3

    @Blogger I think that part [EDIT] -- Pessoal, não posso usar Array nesse caso. O código que coloquei é apenas um exemplo para facilitar o entendimento, meu problema vai bem além disso. shows well that this is not what he needs, IE, it is not clear.

  • 1

    @All that remains is to know if the scope he needs to manage this is global or not. Then perhaps we can reopen the question so that it is profitable.

  • @Guilhermenascimento I find irrelevant - most solutions to a problem have their limitations - and if that was the reason to keep the question closed, I imagine who closed would have asked for this clarification instead of closing

  • 2

    @This blogger is something that has been discussed for a long time, who has experience knows the problems as questions cameleão or X & Y problem that can cause, as I said, if specify the part of the scope (which in Javascript is very important) then blz, It is good to reopen, if it reopens like this possibly (not a statement that will occur, but "may") some problems may occur. Closing is not banning, not deleting forever and the tool has its purpose. In Meta has enough link on the subject.

  • 1

    @Exactly. And I was the first to vote for the closing because of EDIT from the AP that makes it clear that this is not his problem and so he wanted another solution. " I need to add A+B, but I can’t do it by adding up because it won’t solve my problem". There’s not much to help and it just generates noise.

  • 2

    Eduardo Roberto Ferreira, note that the closure is not "repudiation" to your question, simply left a question if the problem is only the "variable" or has more requirements that you did not say, and this "pause" is to clarify before a series of answers appears that does not solve. If you can come forward, we may be able to help with both the solution and the editing of the question.

  • 2

    Eduardo, this doesn’t seem to make sense. An array would solve the problem, or another data structure. The right choice of data structure that will use is what will determine the success of the solution. Even more that you didn’t even contextualize in the question the problem that led you to think that way, creating the variables that way, and even if you have this is not the way, it can mess you up in the future. And you have to consider the scope of the variable as well and it’s certainly not possible to do this in Javascript, at least not in the way you imagine.

  • 1

    That’s right, I think I was clear that I cannot use Array... but OK... If you think that "details are missing", ok... My problem involves semantic interoperability, I use a closed language, and I am integrating a Javascript page with this "closed" system. I solved the problem with the "Eval()" that someone posted yesterday, but today when I entered to mark as answer I found no more.

  • 1

    I could not use Array because I cannot access an array object "on the other side, with the other language"... finally.... I still think I have correctly informed my problem, and that I have made it clear that I cannot use Array.... It’s not "I want to add a+b but I can’t do the sum" It’s: "I want to create variables in a dynamic way, and I can’t use Array" About the scope: It doesn’t have to be global.

  • the answer using Eval() is @devgirl’s - she switched to window[] after they commented that she "didn’t need it", and that apparently "it’s the same" - I particularly, after meeting, prefer Eval()

  • And as much as I know that it’s people helping people on this site, I think your reason for not being able to use an array doesn’t concern anyone - your question was clear without that information, and it will serve other people who have the same question on the site (i, for example, enjoyed learning about it) - I would remove your edits, which was the reason some gave to mark your question as "unclear", and would leave: "how does x?" (the first posted version) and ready - there is nothing clearer than that

  • I also think that @Blogueira... I put Edit only because everyone was talking to use Array... But anyway.... This problem I only had here, in gringo stackoverflow the staff answered without asking questions and wanting to understand the real synthesis of the problem. I believe both the question, and the practical answer helps anyone who queries the question.... To explain my problem would involve very specific business rules that would hardly be harnessed elsewhere.... Anyway... I put, although Generico, everything in a well summarized way, but no less clear or practical.

Show 14 more comments

1 answer

-3


Since you cannot use array:

 for(i=0;i<5;i++){

      window["variavel"+i] = i;

      console.log(window["variavel"+i])
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.