How to create dynamic variables in javascript within a loop

0

I have the following code and wanted to make an improvement on 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 ... is a popup that may have been created. If I know the amount of popup that was created (and I know) I could do more or less this way (like I do in PHP)

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

So I would get all the variables that already exist and I could do something with them ... There is a way for me to keep my code on top {myWindow1.form_1.submit ()}?

Obs. form_1 would also have to be concatenated.

or more or less so

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

    
asked by anonymous 16.06.2017 / 18:09

2 answers

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

This can solve your problem, but I advise you to change logic, because this way your code tends to get worse every time.

    
17.06.2017 / 04:00
0

Because you do not put all the ids in an array, then you loop, and then inside the loop you do not do what you need with the IDs?

// 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)
    
18.06.2017 / 18:58