1

I have an array of objects in which the fields may differ slightly.

when I build a table due to the fact that I do not have the same fields in objects, my table shrinks how can I make a table so that I have an empty line where there are no fields.

Dear colleagues, please tell me how to correctly complete this task.

you can use only pure javascript

const data = [
      {
      "#": 1,
    "First Name": "Wilhelm Conrad",
    "Last Name": "Röntgen",
    "Born Location": "Lennep",
    "Died Location": "Munich, Germany",
    "Gender": "male",
    "Prizes": "yes"
    },
      {
        "#": 1,
    "First Name": "Wilhelm Conrad",
    "Last Name": "Röntgen",
    "Born Location": "Lennep",
    "Gender": "male",
    "Prizes": "yes"
    },
      {
        "#": 1,
    "First Name": "Wilhelm Conrad",
    "Last Name": "Röntgen",
    "Born Location": "Lennep",
    "Died Location": "Munich, Germany",
    "Gender": "male",
    "Prizes": "yes"
    },
      {
        "#": 1,
    "First Name": "Wilhelm Conrad",
    "Last Name": "Röntgen",
    "Born Location": "Lennep",
    "Gender": "male",
    "Prizes": "yes"
    },
      {
        "#": 1,
    "First Name": "Wilhelm Conrad",
    "Last Name": "Röntgen",
    "Born Location": "Lennep",
    "Gender": "male",
    "Prizes": "yes"
    }
]

const keys = Object.keys(data[0])

let tab = document.createElement('table');

const buildTableHeader = () => {
    let thead = document.createElement('thead');
    let tr = document.createElement('tr');

    for (const key of keys) {
        const th = document.createElement("th");
        th.appendChild(document.createTextNode(key));
        tr.appendChild(th);
    }
    thead.appendChild(tr)
    tab.appendChild(thead);

    document.body.appendChild(tab)
}

let tb = document.createElement('tbody');

const buildTableBody = () => {
    for (let a of data) {
        let tr = document.createElement('tr');
        for (let key in a) {
            let td = document.createElement('td');
            let tn = document.createTextNode(a[key]);
            td.appendChild(tn);
            tr.appendChild(td);
        }
        tb.appendChild(tr);
    }
    tab.appendChild(tb);
    document.body.appendChild(tab);
}

buildTableHeader()
buildTableBody()

enter image description here

2 Answers 2

1
const buildTableBody = () => {
    for (let a of data) {
        let tr = document.createElement('tr');
        for(let key of keys) {
            let td = document.createElement('td');
            let tn = document.createTextNode(a[key]);
            td.appendChild(tn);
            tr.appendChild(td);
        }
        tb.appendChild(tr);
    }
    tab.appendChild(tb);
    document.body.appendChild(tab);
}

Change for (let key in a) to for(let key of keys)

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

Comments

1

You're trying to iterate data when some properties don't exist. If you iterate your keys instead you can check if whatever key you have is in your data object. If they don't just create an empty tn.

const buildTableBody = () => {
    for (let a of data) {
        let tr = document.createElement('tr');
        keys.forEach((key) => {
            let td = document.createElement('td');
            let tn = key in a ? document.createTextNode(a[key]) : document.createTextNode('');
            td.appendChild(tn);
            tr.appendChild(td);
        });
        tb.appendChild(tr);
    }
    tab.appendChild(tb);
    document.body.appendChild(tab);
}

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.