javascript - Codition on a last iteration of loop in React JSX? -


i have list of emails need loop through , display in format.

  renderemaillist() {     return this.props.officeemailaddress.map((officeemail) => {         return (           <a key={emailid} href={`mailto:${officeemail}`}>{officeemail}{'; '}</a>           );     }); } 

when rendered

 email@mail.com; email2@mail.com; 

i want remove semicolon @ end of last loop only.

what bets way this?

you have access index , full array within map:

return this.props.officeemailaddress.map((officeemail, index, arr) => {     return (       <a key={emailid} href={`mailto:${officeemail}`}>{officeemail}{index === arr.length - 1 ? '' : '; '}</a>       ); }); 

i added ternary determine if on last element of array and, if are, output empty string.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -