如何在if / else语句中添加if / else语句?

时间:2016-12-06 23:13:19

标签: javascript reactjs meteor

我需要一些帮助来弄清楚如何修改此代码:



export const CommentsListBeijing = ({ comments }) => (
  if (comments.length > 0 ) {
    <ButtonToolbar className="comment-list">
    {comments.map((com) => (
      <CommentsModal key={ com._id } comment={ com } city={com.city} person={com.person} location={com.location} title={com.title} content={com.content} fileLink={com.fileLink} timestamp={com.timestamp} createdBy={com.createdBy}/>
    ))}
  </ButtonToolbar> :
  <Alert bsStyle="warning">No sparks yet. Please add some!</Alert>
);
&#13;
&#13;
&#13;

我想在映射函数中添加一个If / Else语句。

所以

if (com.adminSpark == true) { do something }
else 
do current

我是这种写if / else语句的新手,并且不知道如何将两者结合起来。

感谢您的帮助!我一直在尝试根据收到的反馈添加else / if语句但是我在语句开始时一直收到错误:&#34; imports / ui / components / beijing / comments-list-beijing.js:16: 2:意外的令牌(16:2)&#34;。

我做错了什么?

3 个答案:

答案 0 :(得分:1)

如果你想要一个单行,我会和tenary一起去。

export const CommentsListBeijing = ({ comments }) => (
  if (comments.length > 0 ) {
    <ButtonToolbar className="comment-list">
    {comments.map((com) => (
      return com.adminSpark ? /* return something admin-related */ :      <CommentsModal key={ com._id } comment={ com } city={com.city} person={com.person} location={com.location} title={com.title} content={com.content} fileLink={com.fileLink} timestamp={com.timestamp} createdBy={com.createdBy}/>
    ))}
  </ButtonToolbar> :
  <Alert bsStyle="warning">No sparks yet. Please add some!</Alert>
 }
);

虽然为了提高这段代码的可读性,我觉得它看起来像是:

export const CommentsListBeijing = ({ comments }) => (
  if (comments.length > 0 ) {
    <ButtonToolbar className="comment-list">
    {comments.map((com) => (
      return com.adminSpark ? 
        /* something admin-related */ : 
        <CommentsModal 
          key={ com._id } 
          comment={ com } 
          city={com.city} 
          person={com.person} 
          location={com.location} 
          title={com.title} 
          content={com.content} 
          fileLink={com.fileLink} 
          timestamp={com.timestamp} 
          createdBy={com.createdBy} />
    ))}
    </ButtonToolbar> :
    <Alert bsStyle="warning">No sparks yet. Please add some!</Alert>
   }
 );

更容易阅读的代码几乎总是比“简洁”的代码更有价值。我的两分钱虽然:)

答案 1 :(得分:0)

这应该这样做

export const CommentsListBeijing = ({ comments }) => (
  if (comments.length > 0 ) {
    <ButtonToolbar className="comment-list">
    {comments.map((com) => (
      if (com.adminSpark) {
        // do and return something;
      } else {
        return <CommentsModal key={ com._id } comment={ com } city={com.city} person={com.person} location={com.location} title={com.title} content={com.content} fileLink={com.fileLink} timestamp={com.timestamp} createdBy={com.createdBy}/>
      }
    ))}
  </ButtonToolbar> :
  <Alert bsStyle="warning">No sparks yet. Please add some!</Alert>
);

答案 2 :(得分:0)

我认为,这会有所帮助:

  if (comments.length > 0 ) {
    <ButtonToolbar className="comment-list">
    {comments.map((com) => (
      if (com.adminSpark==true) or if (com.adminSpark){
        // your code ;
      } else {
        return <CommentsModal key={ com._id } comment={ com } city={com.city} person={com.person} location={com.location} title={com.title} content={com.content} fileLink={com.fileLink} timestamp={com.timestamp} createdBy={com.createdBy}/>
      }
    ))}
  </ButtonToolbar>