Lo que quiero hacer: la función que quiero hacer es comparar e insertar datos en el campo de entrada, debe ejecutarse a través de la columna firstName en la tabla y comparar con el campo firstName en la matriz, si alguno de ellos es el mismo, entonces debe imprimir el valor en el campo de entrada.
Problema: no tengo idea de cómo verificar cada fila en la columna firstName en la tabla y compararla con el valor firstName en la matriz.
const student= ["Janet", "Weaver","Adam"]; let text = ""; student.forEach(myFunction); student.forEach(element => console.log(element)); function myFunction(item) { text += item + "<br>"; } //Check the firstName column in table //if(td = student.firstName){ //document.getElementById("janet").innerHTML = student.firstName; //} <!DOCTYPE html> <html> <style> table, th, td { border: 1px solid black; } </style> <body> <h2>User table</h2> <table style="width:100%"> <tr> <th>First name</th> <th>Age</th> <th>Caption</th> </tr> <tr> <td>Janet</td> <td>16</td> <td><input type="text" id="janet" placeholder="Name"></input>*here should appear the name of Janet* </td> <tr> <td>John</td> <td>19</td> <td><input type="text" placeholder="Name"></input> </td> </tr> </table> </body> </html>Probablemente debería recorrer las filas de su tabla, luego comparar el valor de la primera columna con su matriz
EDITAR: document.getElementsByTagName('tr') devuelve una HTMLCollection que debe convertirse en una matriz para permitirle usar forEach
Array.from(document.getElementsByTagName('tr')).forEach(function(row) { let firstTD = row.getElementsByTagName('td').item(0); if (firstTD === null) { return; } let firstname = firstTD.innerHTML; // does the array contain firstname? if (student.indexOf(firstname)>=0) { let input_col = row.getElementsByTagName('td').item(2); let input = input_col.getElementsByTagName('input').item(0); input.value = firstname; } }); Tenga en cuenta que probablemente sería más fácil usar ID o clases en lugar de contar las etiquetas con getElementsByTagName ...