0

hello so what I am trying to do is to check if selectedItem.status is any of these numbers I don't want react do move on into the next blocks.

I have tried to do this another way like for example

 if(selectedItem.status !== ["16", "0", "12", "9", "11", "13", "17", "22"].indexOf(selectedItem.status))

but it didn't work either

    if (
      selectedItem.status !== "16" ||
      "0" || 
      "12" ||
      "9" ||
      "11" ||
      "13" ||
      "17" ||
      "22"
    ) {
      if (!showErrorPage) {
        setShowErrorPage(true);
      }
      if (selectedFile && selectedItem["id"] === selectedFile["id"]) {
        errorCloseHandler();
        setClickedStatusId(0);
        return;
      }
      setSelectedFile(selectedItem);
      setClickedStatusId(selectedItem.id);

      if (!showErrorPage) {
        setHeadCells(
          originalHeaderColumns.map((h) => {
            h.hidden =
              h.id !== "userLogin" && h.id !== "fileName" && h.id !== "status";
            return h;
          })
        );
      } else {
        setHeadCells(originalHeaderColumns);
      }
    } else {
      return;
    }
  • In first block you try to compare array index with status - will not work and only provide bug when index matches status. In second block your first if statement makes no sense – Justinas Dec 17 '21 at 08:12
  • `if (["16", "0", "12", "9", "11", "13", "17", "22"].indexOf(selectedItem.status) !== -1) { // Execute code for status found in array } else { // Execute code for status not found in array }` Also check https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf for proper use cases – Vipin Yadav Dec 17 '21 at 08:13

2 Answers2

2

You can use the array includes method. It will also check for element if it exists in the array and based on that you can apply your logic.

if(["16", "0", "12", "9", "11", "13", "17", "22"].includes(selectedItem.status)){
 // your code here.
}

This should work here.

Shubham J.
  • 601
  • 5
  • 13
-1

This should work:

if (["16", "0", "22",...].includes(selectedItem.status)) {
  ...
}

So should:

 if (
      selectedItem.status !== "16" ||
      selectedItem.status !== "0" ||
      selectedItem.status !== "12" ||
      ...
 )
Spankied
  • 1,626
  • 13
  • 21