Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda