如何检查变量中是否有任何一个大于0

RV.*_*RV. 4 javascript typescript reactjs angular

如何从Typescript中的给定变量检查是否有任何变量大于0?

如何重写下面的代码,使其更优雅/简洁?

checkIfNonZero():boolean{
  const a=0;
  const b=1;
  const c=0;
  const d=0;
  //Regular way would be as below. 
  //How can this use some library instead of doing comparison for each variable
  if(a>0 || b>0 || c>0 || d>0){
   return true;
  }
  return false;
}
Run Code Online (Sandbox Code Playgroud)

Ani*_*ift 5

您可以将变量组合到一个数组中,然后在其上运行一些:

return [a, b, c, d].some(item => item > 0)

  • `some`对此也很有效.+1 (2认同)