Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

417
Visualizações
How to assign multiple variables to different value using 'for loop' in js

I was trying to assign multiple variables that i stored in an array to different variables that i stored in an array using the for loop. Here is what i have tried but didn't get my wanted result. Instead it changed the variable array to the contents in the other array.

let varOne, varTwo, varThree;

let varArray = [varOne, varTwo, varThree]
let valueArray = ['value-one', 'value-two', 
'value-three']

for (let i = 0; i < varArray.length; i++) {
    varArray[i] = valueArray[i]
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This is impossible. Variables can't be modified like this. You just created a copy.

about 4 years ago · Juan Pablo Isaza Relatório

0

I not really understand what do you want to solve, but with destructuring an array you can assing values to multiple variables.

let [varOne, varTwo, varThree] = ['value-one', 'value-two', 
'value-three'];

console.log(varOne, varTwo, varThree);

Or you cam map all the arrays into key-value pairs of an object.

let varArray = ["varOne", "varTwo", "varThree"]
let valueArray = ['value-one', 'value-two', 
'value-three']

let res = {};
varArray.forEach((key, i)=> {
  res[key] = valueArray[i];
});

console.log(res);

// or in an array of object with map...
let res2 = varArray.map((key, i)=>{
    return {[key]: valueArray[i]};
  }
);

console.log(res2);

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Use objects
let data={
 varOne:"value-one",
 varTwo:"value-two",
 varThree:"value-thress"
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda