Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
¿Por qué JavaScript imprime solo el último valor para documentar utilizando document.querySelector o mediante los métodos getElement?

Estoy confundido por qué la matriz de objetos en bucle solo devuelve un valor (uno para la verdad y otro para la falsedad) al usar métodos de documento (métodos querySelector y métodos getElement). Donde como, supongo que espero que todos los objetos regresen.

Aquí está el guión:

 let movies = [ { title: "Spider-Man: No Way Home", rating: 3.5, hasWatched: true }, { title: "Unbroken", rating: 5, hasWatched: true }, { title: "Frozen 2", rating: 5, hasWatched: false }, { title: "Encanto", rating: 5, hasWatched: false }, { title: "Forrest Gump", rating: 5, hasWatched: true } ]; printWatched = document.querySelector(".moviesWatched"); const printNotWatched = document.querySelector(".moviesNotWatched"); for (let i = 0; movies.length > i; i++) { if(movies[i].hasWatched === true) { printWatched.textContent = "You have watched " + '"' + movies[i].title + '"' + " - " + movies[i].rating + " stars"; } else { printNotWatched.textContent = "You have not seen " + '"' + movies[i].title + '"' + " - " + movies[i].rating + " stars"; } }

Pero, si uso document.write, todo es nominal, funciona y devuelve todos los valores dentro de la matriz de objetos...

 let movies = [ { title: "Spider-Man: No Way Home", rating: 3.5, hasWatched: true }, { title: "Unbroken", rating: 5, hasWatched: true }, { title: "Frozen 2", rating: 5, hasWatched: false }, { title: "Encanto", rating: 5, hasWatched: false }, { title: "Forrest Gump", rating: 5, hasWatched: true } ]; const printWatched = document.querySelector(".moviesWatched"); const printNotWatched = document.querySelector(".moviesNotWatched"); for (let i = 0; movies.length > i; i++) { if(movies[i].hasWatched === true) { document.write("<p> You have watched " + '"' + movies[i].title + '"' + " - " + movies[i].rating + " stars <br> </p>"); } else { document.write("<p>You have not seen " + '"' + movies[i].title + '"' + " - " + movies[i].rating + " stars <br> </p>"); } }

Gracias chicos por ayudar.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Está sobrescribiendo el texto anterior con el último texto. Cree una variable y agregue todas esas cadenas. Luego, por fin, use innerHTML

 let movies = [{ title: "Spider-Man: No Way Home", rating: 3.5, hasWatched: true }, { title: "Unbroken", rating: 5, hasWatched: true }, { title: "Frozen 2", rating: 5, hasWatched: false }, { title: "Encanto", rating: 5, hasWatched: false }, { title: "Forrest Gump", rating: 5, hasWatched: true } ]; const printWatched = document.querySelector(".moviesWatched"); const printNotWatched = document.querySelector(".moviesNotWatched"); let moviesWatched = ''; let moviesNotWatched = ''; for (let i = 0; movies.length > i; i++) { if (movies[i].hasWatched) { moviesWatched += `You have watched ${movies[i].title} - ${movies[i].rating} stars`; } else { moviesNotWatched += `You have not seen ${movies[i].title} - ${ movies[i].rating} stars /n`; } } printWatched.innerHTML = moviesWatched; printNotWatched.innerHTML = moviesNotWatched;
 <div class='moviesWatched'></div> <div class='moviesNotWatched'></div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!