How to create dynamic variables in javascript inside a loop

Asked

Viewed 276 times

0

I have the following code and I wanted to improve it.

 if(id == 1)
        if(myWindow1){
        myWindow1.form_1.submit();
        myWindow1 = "";

    }
    if(id == 2)
        if(myWindow2){
            myWindow2.form_2.submit();
            myWindow2 = "";
        }

    if(id == 3)
        if(myWindow3){        
            myWindow3.form_3.submit();
            myWindow3 = "";
        }
    if(id == 4)
        if(myWindow4){            
            myWindow4.form_4.submit();
            myWindow4 = "";
        }
    if(id == 5)
        if(myWindow5){
            myWindow5.form_5.submit();
            myWindow5  = "";
        }

where myWindow ... and a popup that may have been created. If I know the amount of popup that were created( and know ) could do more or less this way (as I do in PHP)

while($contador < $qtdAlgo){
 $nomeVariavel = ${"myWindow". $contador}
 $contador++;
}

So I would take all the variables that already exist and I could do something with them... There’s a way for me to keep my code up there { myWindow1.form_1.Submit() } ?

Obs. form_1 would also have to be concatenated.

or more or less like this

myWindow {counter} . form_{counter}. Ubmit();

  • 1

    You can do myWindow[counter]

  • It would be interesting to see how popups are created.

2 answers

1

Because you don’t put all ids in an array, then you loop, and then inside the loop you don’t do what you need with the ID?

// Array inicial
var IDformularios = [1, 2, 3, 4, 5]

var validaFormularios = function (formularios) {
  var forms = window.IDformularios || []
    if (!forms.length) {
        return new Error('Nao tem formularios')
    } else {
    // LOOP
        forms.forEach(function(form) {
      var nome = 'form'+form
      // EXECUTA LOGICA
          executaLogicaNoFormulario(form)
      // OU CRIA SUA VARIAVEL AQUI
      criaVariavelDinamica(nome)
        })

    }
}

var executaLogicaNoFormulario = function (formulario) {
    if (!formulario) {
        return new Error('Nenhum formulario')
    } else {
        // Logica para o formulario
        console.log('[executaLogicaNoFormulario]: executaLogicaNoFormulario: ', formulario)
    }
}

var novoPopUp = function (nome, id, blababla) {
    if (!nome && !id) return // sem nome e sem ID
    var forms = window.IDformularios || []
    forms.push(id)
    window.open(/* seus popups ... */)
}

var criaVariavelDinamica = function ( nome ) {
  if( window[nome] == null ){
    console.log('nova variavel adicionada:', 'window.'+nome);
    window[nome] = ''
  } else {
    console.log('Variavel ja existe: ', 'window.'+nome);
  }
}

validaFormularios(IDformularios)

1

for(var i=0; i < qtdeForms; i++) { 
    var formCount = i + 1;
    if ( 'myWindow' + formCount in window) {
         window['myWindow' + formCount]['form_' + formCount].submit();
    }
}

This may solve your problem, but I advise you to change your logic, as this way your code tends to get worse and worse.

Browser other questions tagged

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