You can do it this way with while
by checking whether the variables hora
exist and creating the variables var
separating the values with .split
:
var hora1 = '9h - 22h';
var hora2 = '12h - 21h';
var hora = var_ = 1;
while(window['hora'+hora]){ // enquanto existir variáveis iniciando com "hora" + um número sequencial começando do 1
var quebra = window['hora'+hora].split(" - "); // converto em array com dois índices
window['var'+var_] = parseInt(quebra[0]); // primeiro valor pegando apenas o número
window['var'+(var_+1)] = parseInt(quebra[1]); // segundo valor pegando apenas o número
hora++; // incremento em +1
var_ += 2; // incremento em +2
}
console.log(var1, var2, var3, var4);
Remember that to use window[]
, the variables must have an overall scope.
If it’s just these two variables (hora1
and hora2
), can do so:
var hora1 = '9h - 22h'
, hora2 = '12h - 21h'
, quebra = hora1.split(" - ")
, var1 = parseInt(quebra[0])
, var2 = parseInt(quebra[1])
, quebra = hora2.split(" - ")
, var3 = parseInt(quebra[0])
, var4 = parseInt(quebra[1]);
console.log(var1, var2, var3, var4);
Daywison, pay attention to your questions. Looking at your history, you have many unresolved questions with multiple answers, and you keep asking questions without solving the previous ones. This is not good. Value users who spend time answering their questions. Choose an answer and mark . Or if no answer solved the problem, ask who answered to find the solution. Abs!
– Sam