["Первый", "Второй"];
{
"Первый":
0: {id: 8, branch: "Административные службы"}
"Второй":
0: {id: 1, branch: "Администрация"}
1: {id: 2, branch: "Администрация"}
}
const arr = [ '***', '+++' ];
const obj = {
'!!!': [
{ id: 0, text: 'fjdklsfjklds' },
],
'+++': [
{ id: 3, a: '111', b: '!!!' },
{ id: 9, a: '222', b: '???' },
{ id: 27, a: '333', b: ':::' },
{ id: 81, a: '444', b: '...' },
],
'***': [
{ id: 69, text: 'hello, world!!' },
{ id: 187, text: 'fuck the world' },
{ id: 666, text: 'fuck everything' },
],
};
document.body.insertAdjacentHTML('beforeend', arr.map(n => {
const columns = Object.keys(obj[n][0]);
return `
<h1>${n}</h1>
<table>
<thead>
<tr>${columns.map(n => `
<th>${n}</th>`).join('')}
</tr>
</thead>
<tbody>${obj[n].map(row => `
<tr>${columns.map(col => `
<td>${row[col]}</td>`).join('')}
</tr>`).join('')}
</tbody>
</table>`;
}).join(''));
document.body.append(...arr.flatMap(n => {
const columns = Object.keys(obj[n][0]);
const header = document.createElement('h1');
const table = document.createElement('table');
header.textContent = n;
table.createTHead().insertRow().append(...columns.reduce((acc, col) => (
(acc[acc.length] = document.createElement('th')).textContent = col,
acc
), []));
obj[n].forEach(function(row) {
const tr = this.insertRow();
columns.forEach(col => tr.insertCell().textContent = row[col]);
}, table.createTBody());
return [ header, table ];
}));