简体   繁体   中英

How do I return the number value of an if statement if certain conditions are met?

I am working on a function where if certain properties of an array are true then return the number of those conditions that are true.

eg

if (a > b || a < c || d == e || f == e)
{
  return "number of condition that are met";
}

Store the conditions in an array

const conditions = [
   (a,b,c,d,e,f) => a>b,
   (a,b,c,d,e,f) => a<c,
   (a,b,c,d,e,f) => d==e,
   (a,b,c,d,e,f) => f==e,
];

Now, you can use filter to find how many are true

const howManyTrues = conditions.filter(cond => cond(a,b,c,d,e,f) )
if(howManyTrue > 0){
    return howManyTrue;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM