0

I have a function which checks if comments author is current or not.This function returns true or false.

I also have function which checks result of this function and the problem is result function always returns true.

Any suggestions please?

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();
};
8
  • might be your item does not have username property so item.username is undefined and localStorage.getItem("username") is also undefined Commented Jul 10, 2020 at 8:41
  • You're using filter(), which means your comments array changes into a smaller array. So if you have five comments, and two are by the author, the function returns an array of two comments, not true or false. An array is truthy, so it always returns true. I'm also wondering how exactly this is supposed to work in general: shouldn't you check whether the user is the author for each comment individually? Commented Jul 10, 2020 at 8:41
  • @ChrisG could you tell me how to fix it or which method to use please ? Commented Jul 10, 2020 at 8:42
  • I tried to use map but got same result Commented Jul 10, 2020 at 8:42
  • try let checkResult = checkCommentAuthor()[0] Commented Jul 10, 2020 at 8:43

4 Answers 4

3

The Array.filter() function docs returns a new array. So saying every check returns false u get an empty array, and an array (empty or not) translates always to true.

This would be better:

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
};

EDIT: And a little refactoring

const checkCommentAuthor = (): boolean => 
  comments.filter((item: any): boolean => 
    item.username === localStorage.getItem("username")
  ).length > 0; // check on length
Sign up to request clarification or add additional context in comments.

1 Comment

There's a function that already does this: .some
2

your function can be simplified (many parts are useless/not needed). As the others said too, Array.filter() returns an array with all the element that match to true to the filter function body. In case of no match, [] is returned.

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

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

checkCommentAuthorResult();

Comments

2

You can simplify your filter function to the following:

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

and then check if filtered array is empty

if (checkResult.length > 0) {

2 Comments

the return type of checkCommentAuthor is still wrong
@Greedo correct, i forgot it is typescript. changed it to any
1

filter returns an array, which is always truthy, even if it's empty. You can simplify this a bit using some instead, which returns true if at least one item matches:

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

You also don't need this if/else or return type since this already has a boolean equality check:

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

Finally, you can simplify the JSX with a ternary:

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

3 Comments

Thank you very much for the help,Your answer is the closest to the result I want to get.Now I get the following problem.I guess its because of the some method.If one comment belongs to the user whole result is changing to the true.Is there any other method which would fix my problem?
I thought that's what you wanted. What's the issue and what results do you want specifically?
OP actually has a different problem; see my comments below their question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.