in Education by
I need to determine the status of a parent object based on 2 variables of each of its children. I came up with a working solution, but this includes a nested "if-else if-else". Needless to say, it doesn't look very elegant. I was wondering if there is a way to simplify this. I have muddled around with some map/reduce code, but did not get to anything that is more elegant than the code below. const parent = { children: [{ connected: true, online: true }, { connected: true, online: true } ] } // all online & all connected => connected // all online & some connected => partially disconnected // all online & none connected => disconnected // some online => partially offline // none online => offline const onlineArr = parent.children.map(c => c.online); const connectedArr = parent.children.map(c => c.connected); let status; if (!onlineArr.includes(true)) { status = 'Offline'; } else if (!onlineArr.includes(false)) { if (!connectedArr.includes(true)) { status = 'Disconnected'; } else if (!connectedArr.includes(false)) { status = 'Connected'; } else { status = 'Partially disconnected'; } } else { status = 'Partially offline'; } console.log(status); Run code snippetExpand snippet JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
I'd just extract the all/some/none check into a function: const overAll = (array, key, value, /*results in */ all, some, none) => array.every(it => it[key] === value) ? all : (array.some(it => it[key] === value) ? some : none); Then it is as easy as: const status = ( overAll(parent.children, "online", true, undefined, "Partially offline", "Offline") || overAll(parent.children, "connected", true, "Connected", "Partially connected", "not connected") ); But your if/else is already quite clean IMO :)

Related questions

0 votes
    I need to determine the status of a parent object based on 2 variables of each of its children. I ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    When plain text is converted to unreadable format, it is termed as _____________ (a) rotten text (b) raw ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Oct 31, 2021 in Education by JackTerrance
0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    My If Else statement in VB.net is randomly displaying either of the If or ElseIf condition that I made ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 20, 2022 in Education by JackTerrance
0 votes
    I'm learning Haskell in the hope that it will help me get closer to functional programming. Previously, I ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    I am using AA for almost 6 months and I found that by default I am not able to see any lines/dotted lines/ ... to be set in properties? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I am using AA for almost 6 months and I found that by default I am not able to see any lines/dotted lines/ ... to be set in properties? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    int fun(int n) { if(n!=0) { return n – fun(n-5); } else { return n; } } int main(){ int n = 20, z; z = fun(n); printf(“%d”, z); } Select the correct answer from above options...
asked Dec 31, 2021 in Education by JackTerrance
0 votes
    int find(int j) { if(>1X j=find(j/ 10)+(%10); } else { j=0; return j; void main() int i=1000, int k; k=find(i); printf(“%d”, k); Select the correct answer from above options...
asked Dec 29, 2021 in Education by JackTerrance
0 votes
    s=ScHoO12@CoM' k=1en(s) m= for i in range (0 , k ) : if(s ( i ) . isupper ( ) ) : m=m+s ( i ) . ... else : m=m+ bb print ( m )| Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    s=ScHoO12@CoM' k=1en(s) m= for i in range (0 , k ) : if(s ( i ) . isupper ( ) ) : m=m+s ( i ) . ... else : m=m+ bb print ( m )| Select the correct answer from above options...
asked Dec 14, 2021 in Education by JackTerrance
0 votes
    8. Rewrite the following using ternary operator. (a) if (bill>10000) discount=bill*10.0/100; else discount=bill*5.0/100; Select the correct answer from above options...
asked Nov 30, 2021 in Education by JackTerrance
0 votes
    I created update() method which is using JPA. It looks like this: public boolean update(Programy program) ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 13, 2022 in Education by JackTerrance
0 votes
    Simplify the Boolean expression using algebraic method (A’+B)(A+B) Select the correct answer from above options...
asked Dec 12, 2021 in Education by JackTerrance
0 votes
    How does Kubernetes simplify containerized Deployment?...
asked Oct 2, 2021 in Technology by JackTerrance
...