Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commas Appearing in Javascript rendered HTML

I am rendering HTML with Javascript and Contentful.

My code:

contentfulClient.getEntries({
   content_type: PRODUCT_CONTENT_TYPE_ID,
   order: '-fields.dateRated'
})
.then(function(entries) {
   container.innerHTML = renderProducts(entries.items)
})

function renderProducts(products) {
   return '<table id="film-table"><tr><th>Name</th><th>Rating</th></tr><tr><td>Check back tomorrow</td><td class="rating-cell">🆕</td></tr>' +
   products.map(renderSingleProduct) +
   '</table>'
}

However, when I test this a comma appears above the table for each entry: commas above table.

Thanks for the help.

like image 973
AlexanderRohrig Avatar asked Oct 19 '25 13:10

AlexanderRohrig


1 Answers

The problem here is that the .map function returns an array. So when you're adding products.map(renderSingleProduct) to the existing string it is turning this array into a string, which includes the , as a separator.

If you add a .join('') call at the end everything should be fine as this turns the array back into a string with the provided separator.

function renderProducts(products) {
   return '<table id="film-table"><tr><th>Name</th><th>Rating</th></tr><tr> 
          <td>Check back tomorrow</td><td class="rating-cell">🆕</td></tr>' 
            +
            products.map(renderSingleProduct).join('') +
          '</table>'
          }
like image 194
Robban Avatar answered Oct 21 '25 03:10

Robban



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!