12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- let persons = [
- {
- name: 'Sergey',
- surname: 'Honcharenko',
- age: 28,
- isMarried: false,
- },
- {
- name: 'Andrew',
- surname: 'Klymchuk',
- middleName: 'Anatoliyovych',
- haveAJob: true,
- },
- {
- name: 'Alexander',
- surname: 'Slobodian',
- sex: 'male',
- hasAPet: false,
- }
- ];
- let personsKeys = [];
- for (let key in persons) {
- for (let value in persons[key]) {
- personsKeys.push(value);
- }
- }
- let personsUniqueKeys = new Set(personsKeys);
- let str = "<table>";
- str += `<thead><tr>`;
- personsUniqueKeys.forEach(item => {
- str += `<th>${item}</th>`;
- });
- str += "</thead></tr>";
- for (let key in persons) {
- str += `<tr>`;
- for (let personsKey in [...personsUniqueKeys]) {
- if(Object.keys(persons[key]).includes([...personsUniqueKeys][personsKey])) {
- str += `<td>${persons[key][[...personsUniqueKeys][personsKey]]}</td>`
- } else {
- str += `<td>-</td>`
- }
- }
- str += "</tr>";
- }
- str += "</table>";
- document.write(str);
|