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

167
Vistas
How can I make the serial number of the dynamic table row to be equal to the serial number of the object in the array?

I am trying to create a table where each row will be an object in an array. Logically it works, but it gives me the number of rows in the table equal to the number of objects in the array each time I perform the function. Instead, I need the serial number of the row to be equal to the serial number of the object in the array. How can I achieve this? (* beginner's class project. can't use advanced js/libraries).

<html>

<head>
  <style>
    table,
    th,
    td,
    tr {
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <form>
    <input id="firstname" type="text" placeholder="firstname"></input><br>
    <input id="lastname" type="text" placeholder="lastname"></input><br>
  </form>
  <button onclick="myFunc()">calc</button>
  <div></div>
</body>
<script>
  let properties = [
    'firstname',
    'lastname',
  ]
  let form = document.querySelector('form')
  let div = document.querySelector('div')
  let user = {}
  let users = []

  function myFunc() {
    users.push(user)
    for (let prop of properties) {
      user[prop] = form.elements[prop].value
    }
    for (let user of users) {
      //HERE is my problem. do i need to create separate function to calculate nubers and another one to render the table? 
      let tr = document.createElement('tr')
      for (let key in user) {
        let td = document.createElement('td')
        td.innerHTML = user[key]
        tr.appendChild(td)
      }
      let table = document.createElement('table')
      table.appendChild(tr)
      div.appendChild(table)
      form.reset()
      console.log(users.length)
    }
  }
</script>

</html>

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

I'm not sure if I understood you correctly but I think you were pretty close. The thing is that, how it currently is, you never delete items from the object, you only add. So you don't have to render the whole table from the beginning therefore you don't need the for loop.

<html>

<head>
  <style>
    table,
    th,
    td,
    tr {
      border: 1px solid black;
    }
  </style>
</head>

<body>
  <form>
    <input
      id="firstname"
      type="text"
      placeholder="firstname"></input><br>
    <input
      id="lastname"
      type="text"
      placeholder="lastname"></input><br>
  </form>
  <button
    onclick="myFunc()">calc</button>
  <div>
  </div>
</body>
<script>
  let properties = [
    'firstname',
    'lastname',
  ]
  let form = document.querySelector('form')
  let div = document.querySelector('div')
  let user = {}
  let users = []

  function myFunc() {
    users.push(user)
    for (let prop of properties) {
      user[prop] = form.elements[prop].value
    }
    let tr = document.createElement('tr')
    for (let key in user) {
      let td = document.createElement('td')
      td.innerHTML = user[key]
      tr.appendChild(td)
    }
    let table = document.querySelector("table")
    if (table === null) {
      table = document.createElement('table');
    }
    table.appendChild(tr)
    div.appendChild(table)
    form.reset()
    console.log(users.length);
  }
</script>

</html>

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