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);
1 Answer 1
As a function
When you write code, even as an example, always write it as a function. A function is returnable, and thus can be written differently than non returnable flat execution.
Comment code mismatch
"Comments are just lies in waiting."
...by unknown guru.
Your comments do not match the code. The comments specify lowercase status, your code capitalists the status. Which is correct is anyone's guess. (削除) I will assume the comments are correct and that the status is formatted upon display. (It makes the solution simpler) (削除ここまで) Changed my mind and will assume the code has been tested and is correct.
Inefficiency === Inelegance
The nested statements are not inelegant (in a function it would not need any else
statements), its the two Array.map
and three Array.includes
that are very inefficient for the task at hand, which to me is ugly inelegance.
Solution
It is the number of online, connected children that you need to know.
If the number of online children
- is the same as the number of children then all are online.
- is the less than as the number of children and not zero then some are online.
- is zero then none are online
The same applies for connected children.
Thus count the two types and use the counts to return the status, as follows
function connectionStatus(clients) {
const count = clients.length;
var onC = 0, conC = 0;
for (const {connected, online} of clients) {
conC += connected;
onC += online;
}
if (onC === count) {
if (conC === count) { return "Connected" }
return conC ? "Partially disconnected" : "Disconnected";
}
return onC ? "Partially offline" : "Offline";
}
connectionStatus(parent.children);
-
\$\begingroup\$ I would replace var onC = 0, conC = 0; with const metrics = { connected: 0, online: 0 }; to gain additional readability. And a chained ternary operator to return the result string perhaps. \$\endgroup\$dfhwze– dfhwze2019年08月19日 17:27:58 +00:00Commented Aug 19, 2019 at 17:27
-
\$\begingroup\$ @dfhwze The line between personal preference and idiomatic style often finds me writing code I am not comfortable with. I prefer nested ternaries, but don't like the down votes. Obj
metric
to encap names would decrease readability, it just adds noise. Reassigning names in loop .{connected: con, online: on} of clients
then varsconnected += con; online += on
rather thanconC
andonC
but was unsure re browser support. Most annoying was the strings caps. All lowercase was is my pref, not really the functions role to format bio speak, hand that to CSS or higher level code \$\endgroup\$Blindman67– Blindman672019年08月19日 19:08:37 +00:00Commented Aug 19, 2019 at 19:08 -
\$\begingroup\$ It's a pitty ppl downvote nested ternaries. They are compact and don't hurt readability at all :( \$\endgroup\$dfhwze– dfhwze2019年08月19日 19:14:56 +00:00Commented Aug 19, 2019 at 19:14