Nika Roffy
Nika Roffy

Reputation: 941

Function always returning true in Javascript ReactJS

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

Upvotes: 0

Views: 772

Answers (4)

Stutje
Stutje

Reputation: 844

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

Upvotes: 3

Apostolos
Apostolos

Reputation: 10463

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) {

Upvotes: 2

Greedo
Greedo

Reputation: 3549

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();

Upvotes: 2

Nicky McCurdy
Nicky McCurdy

Reputation: 19534

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

Upvotes: 1

Related Questions