Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在此if/else语句中添加if/else语句?_Javascript_Reactjs_Meteor - Fatal编程技术网

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

Javascript 如何在此if/else语句中添加if/else语句?,javascript,reactjs,meteor,Javascript,Reactjs,Meteor,我需要一些帮助,了解如何修改此代码: export const CommentsListBeijing=({comments})=>( 如果(comments.length>0){ {comments.map((com)=>( ))} : 还没有火花。请添加一些! );这应该可以 export const CommentsListBeijing = ({ comments }) => ( if (comments.length > 0 ) { <ButtonToo

我需要一些帮助,了解如何修改此代码:

export const CommentsListBeijing=({comments})=>(
如果(comments.length>0){
{comments.map((com)=>(
))}
:
还没有火花。请添加一些!
);
这应该可以

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>
);
export const CommentsListBeijing=({comments})=>(
如果(comments.length>0){
{comments.map((com)=>(
if(com.adminSpark){
//做并归还某物;
}否则{
返回
}
))}
:
还没有火花。请添加一些!
);

我认为,这将有助于:

if(comments.length>0){
{comments.map((com)=>(
如果(com.adminSpark==true)或如果(com.adminSpark){
//你的代码;
}否则{
返回
}
))}

如果你喜欢一行,我就选十行

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})=>(
如果(comments.length>0){
{comments.map((com)=>(
返回com.adminSpark?/*返回与管理员相关的内容*/:
))}
:
还没有火花。请添加一些!
}
);
虽然为了提高代码的可读性,我还是希望它看起来像:

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>
   }
 );
export const CommentsListBeijing=({comments})=>(
如果(comments.length>0){
{comments.map((com)=>(
返回com.adminSpark?
/*与管理员相关的内容*/:
))}
:
还没有火花。请添加一些!
}
);

易于阅读的代码几乎总是比“简洁”的代码更有价值。不过,我的两分钱:)

非常感谢,所以新的“一行”代码是不可能的语句?当然是这样,但该版本比此版本更容易理解和更灵活:
return com.adminSpark?/*其他内容*/:
Hmmm…在尝试实现代码时,我遇到一个错误:“imports/ui/components/beijing/comments list beijing.js:23:8:意外标记(23:8)”这是指if语句开始的部分。我不明白为什么。非常感谢您的回复!出于某种原因,我一直在if语句开始的地方遇到错误:“imports/ui/components/beijing/comments list beijing.js:16:2:Unexpected token(16:2)”为什么?顺便说一句,第16行指的是“if(comments.length>0){”开始。是的,我猜了很多。你需要关闭if的括号。我现在就编辑答案。