Answer the question
In order to leave comments, you need to log in
How can I shorten the record of creating identical elements?
Multiple tds
are added to tr . Here's how it's written now:
function createPayment({id, sum, system, date})
{
const payment = document.createElement('tr');
payment.classList.add('item', 'block-sr-2', 'all');
payment.dataset.ref = "mixitup-target";
const addPaymentCell = value => {
const td = document.createElement("td");
td.innerText = value;
return td;
};
payment.append(addPaymentCell(id));
payment.append(addPaymentCell(sum));
payment.append(addPaymentCell(system));
payment.append(addPaymentCell(date));
}
payment.append(addPaymentCell(id));
payment.append(addPaymentCell(sum));
payment.append(addPaymentCell(system));
payment.append(addPaymentCell(date));
function createPayment({id, sum, system, date}) {
const payment = document.createElement('tr');
payment.classList.add('item', 'block-sr-2', 'all');
payment.dataset.ref = "mixitup-target";
const object = {id, sum, system, date};
object.forEach(value => {
const td = document.createElement("td");
td.innerText = value;
payment.append(td);
});
}
Answer the question
In order to leave comments, you need to log in
We remove the destructuring of the parameter (well, or you can leave it, then the quotes from the array elements will need to be removed ( 'id'
-> id
, 'sum'
-> sum
, ...), and instead paymentData[n]
it will be just n
), instead of ten lines we write three:
payment.innerHTML = [ 'id', 'sum', 'system', 'date' ]
.map(n => `<td>${paymentData[n]}</td>`)
.join('');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question