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

136
Visualizações
Show array from getElementById

I would like to use the document.getElemntById as a "name" of listing array

function aircraft(operator) {
  var typ = document.getElementById('typ').value;

  var B738 = ['39.5', '35.8', '12.5', '2', '79000', '2930', 'M0.785', '189'];


  document.getElementById('dlugosc').textContent = "Długość: " + B738[0] + 'm';
  document.getElementById('szerokosc').textContent = "Szerokość: " + B738[1] +'m';
  document.getElementById('wysokosc').textContent = "Wysokość: " + B738[2] +'m';
  document.getElementById('silniki').textContent = "Ilość silników: " + B738[3];
  document.getElementById('mtow').textContent = "MTOW: " + B738[4] + 'kg';
  document.getElementById('zasieg').textContent = "Zasięg: " + B738[5] + 'nm';
  document.getElementById('predkosc').textContent = "Prędkość max.: " + B738[6];
  document.getElementById('pasazerowie').textContent = "Pasażerowie max.: " + B738[7];
}

And I can list all from B738 array, but I would like to have more arrays and use diffrent names of listing arrays like var typ, but it doesnt work

function aircraft(operator) {
  var typ = document.getElementById('typ').value;

  var B738 = ['39.5', '35.8', '12.5', '2', '79000', '2930', 'M0.785', '189'];
  var A320 = ['39.5', '35.8', '12.5', '2', '79000', '2930', 'M0.785', '189'];

  document.getElementById('dlugosc').textContent = "Długość: " + typ[0] + 'm';
  document.getElementById('szerokosc').textContent = "Szerokość: " + typ[1] +'m';
  document.getElementById('wysokosc').textContent = "Wysokość: " + typ[2] +'m';
  document.getElementById('silniki').textContent = "Ilość silników: " + typ[3];
  document.getElementById('mtow').textContent = "MTOW: " + typ[4] + 'kg';
  document.getElementById('zasieg').textContent = "Zasięg: " + typ[5] + 'nm';
  document.getElementById('predkosc').textContent = "Prędkość max.: " + typ[6];
  document.getElementById('pasazerowie').textContent = "Pasażerowie max.: " + typ[7];
}
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can't directly use the value of typ as a variable in the way you're attempting, but you can use it as an indexer on an object. Make your arrays properties on an object:

var myArrays = {
  B738: ['39.5', '35.8', '12.5', '2', '79000', '2930', 'M0.785', '189'],
  A320: ['39.5', '35.8', '12.5', '2', '79000', '2930', 'M0.785', '189']
};

Then you can use typ to reference those properties:

document.getElementById('dlugosc').textContent = "Długość: " + myArrays[typ][0] + 'm';
about 4 years ago · Santiago Trujillo 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