0

i have a table into which i need to add rows by counting the number of elements i have in my object in javascript.I have an object that has normal keys and an array of values.

     userobject={    
                 ID: [1,2,3]
                 IP_Address: ["12.21.12.321","52.23.12.312","12.32.12.321"]
                 Server_Name: ["test1","test2","test3"]}

This is how my object looks like.I need to add each element in the array into a column on each row in the table. How can i automatically generate this.

ID|  IP ADDRESS |SERVERNAME|
1 | 12.21.12.321|   test1  |
2 | 52.23.12.312|   test2  |
3 | 12.32.12.321|   test3  |

and so on. Data needs to be inserted from the array of values into each row.I have already created the table header i.e.column names. i just need help to insert each value one by one into each row.And also if a person wants to delete a row
a row has to be deleted based on the number of elements in the array.

Can someone please help me!?

2 Answers 2

1

You can automatically generate table-rows by iterating over the data and including the items at the respective indices. To delete a row, you can add a button that deletes its closest row. Here's an example:

const userobject = {
  ID: [1,2,3],
  IP_Address: ["12.21.12.321","52.23.12.312","12.32.12.321"],
  Server_Name: ["test1","test2","test3"]
};
const [table] = document.getElementsByTagName('table');
const deleteHandler = ({ target }) => target.closest('tr').remove();
const btnTmpl = document.createElement('button');
btnTmpl.textContent = 'delete';

userobject.ID.forEach((id, i) => {
  const tr = table.insertRow();
  const btn = btnTmpl.cloneNode(true);  
  btn.onclick = deleteHandler;  
  tr.insertCell().textContent = id;
  tr.insertCell().textContent = userobject.IP_Address[i];
  tr.insertCell().textContent = userobject.Server_Name[i];
  tr.insertCell().append(btn);
  table.append(tr);
});
table {
border-collapse: collapse;
}

td, th {
border: 1px solid black;
}
<table>
<theader><tr><th>ID</th><th>IP</th><th>Server</th></tr></theader>
</table>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you this just saved a lot of time.
0

Here is a code proposition, I hope you can adapt it to your need

const header = document.getElementById('header');
const tbody = document.getElementById('tbody');

const userobject = {
  ID: [1, 2, 3],
  IP_Address: ["12.21.12.321", "52.23.12.312", "12.32.12.321"],
  Server_Name: ["test1", "test2", "test3"],
};


Object.keys(userobject).forEach(key => {
  header.innerHTML += `<th>${key}</th>`;
});

for (let i = 0, c = userobject.ID.length; i < c; i++) {
  const tr = document.createElement('tr');
  Object.values(userobject).forEach(array => {
    tr.innerHTML += `<td>${array[i]}</td>`;
  });

  tbody.append(tr);
}
<table>
  <thead>
    <tr id="header"></tr>
  </thead>
  <tbody id="tbody"></tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.