0

I have a bunch of names I'm adding to an array:

for (const approve of requestApproval[strings.Approve]) {
    approved.push(
        approve.Title + '<br/>'
    );
}

The array:

enter image description here

I then put it into an email as HTML:

<center>
    <table border="0" cellpadding="5" cellspacing="5" style = "border-collapse: collapse;">
        <tr>
            <th>Approved</th>
            <th>Abstained</th>
            <th>Not Approved</th>
        </tr>
        <tr>
            <td align="center">${approved}</td>
            <td align="center">${abstained}</td>
            <td align="center">${disapproved}</td>
        </tr>
    </table>
</center>

The result is a comma is inserted from somewhere:

enter image description here

How can I either stop the comma or remove the comma?

1
  • 3
    approved.join("") - the comma is there because its the default behavior of Array.toString()) Commented Apr 4, 2019 at 14:11

2 Answers 2

1

When you do this in your template literal:

<td align="center">${approved}</td>

... you silently convert the array approved to a string, whereby the array elements are delimited with a comma.

To avoid that, either:

  1. don't build an array, but a string:

    let approved = ""; // string!
    for (const approve of requestApproval[strings.Approve]) {
        approved += approve.Title + '<br/>';
    }
    

    Or

  2. convert the array in a controlled way, using the empty string as delimiter:

    <td align="center">${approved.join("")}</td>
    
Sign up to request clarification or add additional context in comments.

Comments

1

You need to convert your array to a string by joining it with empty strings (.join('')), otherwise, JavaScript will automatically join it with commas when converting it to a string.

You can also replace your for loop with Array.prototype.map():

const approved = requestApproval[strings.Approve].map(x => x.Title + '<br/>').join('');

or (but removes the last <br>):

const approved = requestApproval[strings.Approve].map(x => x.Title).join('<br/>');

Then insert the string in your html template string:

<td align="center">${approved}</td>

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.