javascript 有没有改进if语句的方法

4ktjp1zp  于 2023-01-19  发布在  Java
关注(0)|答案(1)|浏览(117)

我在ReactJS上得到了这个if语句,但我认为这可能会改进和更好。而且,我认为switch语句也会是一样的,所以我真的很想知道是否有其他方法。

setCosmeticsall(res.data.data);
  if (type === "outfit") {
    setCosmeticsall([]); //reset data of array
    setCosmeticslength(); //reset results to 0
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "outfit")
        .filter((itemsname) => itemsname.name.includes(search))
    ); // set data to a specific type 
    setCosmeticslength(cosmeticsall.length); //set amout of results to show
  } else if (type === "wrap") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "wrap")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "banner") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "banner")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "spray") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "spray")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "emoji") {
    setCosmeticsall([]);
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "emoji")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "pickaxe") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "pickaxe")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "glider") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "glider")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "loadingscreen") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "loadingscreen")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (type === "emote") {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data
        .filter((items) => items.type.value === "emote")
        .filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  } else if (search.length > 0) {
    setCosmeticsall([]);
    setCosmeticslength();
    setCosmeticsall(
      res.data.data.filter((itemsname) => itemsname.name.includes(search))
    );
    setCosmeticslength(cosmeticsall.length);
  }

这是if语句执行操作的过滤器数据,其中Search变量表示用户在输入搜索中键入的内容。

cosmeticsall.filter(type.value === "All" || type.value!=="All" ? (items) => items.type.value : (items)=> items.type.value === type).filter((itemsname) => itemsname.name.includes(search)).map()
kwvwclae

kwvwclae1#

尽量让你的代码变干。

if (isValidType(type)) {
  setCosmeticsall([]); 
  setCosmeticslength(); 
  setCosmeticsall(
    res.data.data
      .filter((items) => items.type.value === type)
      .filter((itemsname) => itemsname.name.includes(search))
  );
  setCosmeticslength(cosmeticsall.length); //set amout of results to show
} else {
  // handle the case where "type" is a bad/unknown/undefined value
}

相关问题