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
Post a Comment