【问题标题】:Function always returning true in Javascript ReactJS函数在 Javascript ReactJS 中总是返回 true
【发布时间】:2020-10-30 23:10:38
【问题描述】:

我有一个函数可以检查 cmets 作者是否是当前的。这个函数返回真或假。

我也有检查这个函数结果的函数,问题是结果函数总是返回真。

有什么建议吗?

const checkCommentAuthor = (): boolean => {
  return comments.filter((item: any): boolean => {
    if (item.username === localStorage.getItem("username")) {
      return true;
    } else {
      return false;
    }
  });
};

const checkCommentAuthorResult = (): any => {
  let checkResult = checkCommentAuthor();
  if (checkResult) {
    return <IsCommentAuthor / > ;
  } else {
    return <IsNotCommentAuthor / > ;
  }
};

return checkCommentAuthorResult();
};

【问题讨论】:

  • 可能是您的项目没有用户名属性,所以item.username 未定义,localStorage.getItem("username") 也未定义
  • 您正在使用filter(),这意味着您的 cmets 数组变为一个较小的数组。因此,如果您有五个 cmets,其中两个是作者的,则该函数返回一个包含两个 cmets 的数组,而不是 true 或 false。数组是真的,所以它总是返回真。我还想知道这通常应该如何工作:您不应该单独检查用户是否是每个评论的作者吗?
  • @ChrisG 你能告诉我如何修复它或使用哪种方法吗?
  • 我尝试使用地图但得到相同的结果
  • 试试let checkResult = checkCommentAuthor()[0]

标签: javascript reactjs


【解决方案1】:

Array.filter() 函数 docs 返回一个新数组。所以说每张支票都返回false,你得到一个空数组,一个数组(无论是否为空)总是转换为true

这样会更好:

const checkCommentAuthor = (): boolean => {
  return comments.filter((item: any): boolean => {
    if (item.username === localStorage.getItem("username")) {
      return true;
    } else {
      return false;
    }
  }).length > 0; // check on length
};

编辑: 还有一点重构

const checkCommentAuthor = (): boolean => 
  comments.filter((item: any): boolean => 
    item.username === localStorage.getItem("username")
  ).length > 0; // check on length

【讨论】:

  • 有一个函数已经这样做了:.some
【解决方案2】:

您可以将过滤器功能简化为以下内容:

const checkCommentAuthor = (): any => {
  return comments.filter((item: any): any => {
    item.username === localStorage.getItem("username")
  });
};

然后检查过滤后的数组是否为空

if (checkResult.length > 0) {

【讨论】:

  • checkCommentAuthor 的返回类型还是错误
  • @Greedo 正确,我忘了它是打字稿。将其更改为any
【解决方案3】:

filter 返回一个数组,该数组始终为真,即使它为空。您可以使用some 来简化这一点,如果至少有一项匹配,则返回true

const checkCommentAuthor = (): boolean => {
  return comments.some((item: any): boolean => {
    if (item.username === localStorage.getItem("username")) {
      return true;
    } else {
      return false;
    }
  });
};

你也不需要这个 if/else 或返回类型,因为它已经有一个布尔相等检查:

const checkCommentAuthor = () => {
  return comments.some(
    (item: any) => item.username === localStorage.getItem("username")
  );
};

最后,你可以用一个三元来简化 JSX:

const checkCommentAuthorResult = () => {
  let Result = checkCommentAuthor() ? IsCommentAuthor : IsNotCommentAuthor;
  return <Result />;
};

【讨论】:

  • 非常感谢您的帮助,您的回答与我想要得到的结果最接近。现在我遇到以下问题。我猜是因为some 方法。如果有评论属于用户,整个结果正在变为 true。还有其他方法可以解决我的问题吗?
  • 我认为这就是你想要的。有什么问题,您具体想要什么结果?
  • OP 其实有不同的问题;在他们的问题下方查看我的 cmets。
【解决方案4】:

您的功能可以简化(许多部分无用/不需要)。 正如其他人所说,Array.filter() 返回一个数组,其中包含与过滤器函数体匹配的所有元素。如果不匹配,则返回[]

const checkCommentAuthor = (): boolean => {
  return comments.filter((item) => item.username === localStorage.getItem("username")).length > 0;
};

const checkCommentAuthorResult = (): any => {
  return checkCommentAuthor() ? <IsCommentAuthor / > : <IsNotCommentAuthor / > ;
};

checkCommentAuthorResult();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-06
    • 2022-06-28
    • 2019-11-23
    • 1970-01-01
    相关资源
    最近更新 更多