The JavaScript Rest Parameter, Pt. 2
Here's another example, this time it's accessing a string value inside of an array of objects:
function getLabelsHtml(text, sender, ...staffObjs) {
return staffObjs.map(staffObj =>
`<div class="label-card">
<p>Dear ${staffObj.name}</p>
<p>${text}</p>
<p>Best wishes,</p>
<p>${sender}</p>
</div>`
).join('')
}
Set a couple of string variables:
const text = 'Thank you for all your hard work throughout the year! ' const sender = 'Tom'
And now we call the function below, which renders the HTML:
document.getElementById('labels-container').innerHTML = getLabelsHtml(
text,
sender,
{name: 'Sally'},
{name: 'Mike'},
{name: 'Rob'},
{name: 'Harriet'}
)
And here's the output (HTML and CSS excluded):
