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

288
Vistas
Insert a variable into an objects name in Javascript

If you look at the JSON posted further below, I have several objects named row0, row1, row2 etc...

The code I posted further below works but not as intended. The problem lies with the following line:

for (let key in movies.row1) {

I hard coded row1 but I really want to achieve is row1 then row2 then row3 etc...

So I want to insert the value of i into the name of the object after the word row...

movies.rowi <--- i needs to be a variable not the letter i.

I tried a few guesses from googling and stackoverflow like movies.row${[i]} but nothing worked. Also, tried spread operator something like movies.row[1...6] but its invalid.

  function display_all_movies(movies) {

    for (i = 0; i < movies.row_count.count; i++) {

    for (let key in movies.row1) {

       document.getElementById("moviesList").innerHTML += `Key: ${key}` + `Value: ${movies.row1[key]}` + '<br>';

    }
  }
}

movies is an object in JSON format, which is validated. Movies JSON is as follows:

{"row0" : {"title":"Avengers","imgName":"Avengers.jpeg","addedBy":"MrCoolDude","stars":"4"},"row1" : {"title":"Spiderman","imgName":"Spiderman.jpeg","addedBy":"TheNumber2Dude","stars":"5"},"row2" : {"title":"Conair","imgName":"conair.png","addedBy":"Eagleuser","stars":"3"}, "row_count" : {"count" : "4"},"row3" : {"title":"Robin","imgName":"Robin.jpeg","addedBy":"UserBatman","stars":"4"}}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use bracket notation for dynamic property access.

for (let i = 0; i < movies.row_count.count; i++) {
    for (let key in movies[`row${i}`]) {
        document.getElementById("moviesList").innerHTML += `Key: ${key}` + `Value: ${movies[`row${i}`][key]}` + '<br>';
    }
}
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