<!DOCTYPE html>
<
html
lang
=
"en"
>
<
body
>
<
script
>
const data = [
{ name: 'Rahul', age: 25, city: 'New Delhi' },
{ name: 'Vijay', age: 30, city: 'Muzaffarpur' },
{ name: 'Gaurav', age: 22, city: 'Noida' },
];
function createTableWithForEach() {
const table = document.createElement('table');
table.setAttribute('border', '1');
const headerRow = document.createElement('tr');
Object.keys(data[0]).forEach(key => {
const th = document.createElement('th');
th.appendChild(document.createTextNode(key));
headerRow.appendChild(th);
});
table.appendChild(headerRow);
data.forEach(item => {
const row = document.createElement('tr');
Object.values(item).forEach(value => {
const td = document.createElement('td');
td.appendChild(document.createTextNode(value));
row.appendChild(td);
});
table.appendChild(row);
});
document.body.appendChild(table);
}
createTableWithForEach();
</
script
>
</
body
>
</
html
>