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

202
Visualizações
How can I loop through the array of objects and display on dropdown

I'm new to Javascript and I'd appreciate any help I can get. I'm currently working on a project and trying to loop over the select dropdown to display the lenses I have on my array but am encountering errors and i'm stuck.

The page loads fine with the HTML content but the select option which displays lenses so users can choose from has been my issue. The dropdown is from an array of camera lenses like this:-

lenses: Array(2)
 0: "35mm 1.4"
 1: "50mm 1.6"

Here's the Javascript code and the innerHTML:-

const urlParam = new URLSearchParams(window.location.search);
let productId = urlParam.get("id");
let product = null;

  // FETCH API

fetch("http://localhost:3000/api/cameras/" + productId)
    .then((response) => {
        return response.json();
    })

.then((data) => {
    let lenses = "";

    for (const cameraSelect of data.lenses) {
        lenses += `<option>${lenses}</option> `; //select camera lenses
    }


    //HTML container
      let innerHTML = `
      <div class='col'>
          <div class="card">
          <img src=${data.imageUrl} class="card-img-top">
          <div class="card-body"></div>
              <h3 class="card-title" >${data.name}</h3>
              <p class="card-text">${data.price}€</p>
              <select id="lenses">
                 <option>${data.lenses}</option>
              </select>
              <h2>result</h2>
          </div>
      </div>
          `;

      document.getElementById("productList").insertAdjacentHTML('beforeend',innerHTML) ;

      console.log(data);
     
})

.catch(function () {
  window.alert('oops something went wrong! Try again.');
});   

I'd appreciate any help i can get, thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First of all you need to use the cameraSelect when you are building the lenses string.

Then you need to put that populated lenses variable in the innerHTML variable, for the select options.

const urlParam = new URLSearchParams(window.location.search);
let productId = urlParam.get("id");
let product = null;

// FETCH API

fetch("http://localhost:3000/api/cameras/" + productId)
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    let lenses = "";

    for (const cameraSelect of data.lenses) {
      lenses += `<option>${cameraSelect}</option> `; //select camera lenses
    }

    //HTML container
    let innerHTML = `
          <div class='col'>
              <div class="card">
              <img src=${data.imageUrl} class="card-img-top">
              <div class="card-body"></div>
                  <h3 class="card-title" >${data.name}</h3>
                  <p class="card-text">${data.price}€</p>
                  <select id="lenses">
                     ${lenses}
                  </select>
                  <h2>result</h2>
              </div>
          </div>
              `;

    document.getElementById("productList").insertAdjacentHTML('beforeend', innerHTML);

    console.log(data);
  })
  .catch(function() {
    window.alert('oops something went wrong! Try again.');
  });
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