1 回答
TA贡献1757条经验 获得超8个赞
所以,看起来你的逻辑实际上是这样的:
您有一组状态对象。
如果任何状态为“InProgress”或“Pending”,则返回“InProgress”
如果所有状态都相同且为“失败”、“取消”或“DidNotRun”,则返回该值。
如果任何状态为“失败”、“取消”或“DidNotRun”,则返回“部分完成”
否则,返回“完成”
因此,如果您能弄清楚如何通过一次遍历数组而不是 5 次部分遍历数组来完成所有这些检查,将会获得巨大的执行效率。
这是一种方法:
const doneStatuses = new Set(["Failed", "Canceled", "DidNotRun", "Completed", "InProgress"]);
function getComputedStatus(list) {
const allStatuses = new Set();
// collect all status values in a Set object
for (let status of list) {
allStatuses.add(status.statusName);
}
// if all the statusName values were the same and they were
// "Failed", "Canceled", "DidNotRun", "InProgess" or "Completed"
// then return that specific status
if (allStatuses.size === 1) {
let theStatus = Array.from(allStatuses)[0];
if (doneStatuses.has(theStatus)) {
return theStatus;
}
}
// if any status was "InProgress" or "Pending", return "InProgress"
if (allStatuses.has("InProgress") || allStatuses.has("Pending")) {
return "InProgress";
}
// If any status was "Failed", "Canceled" or "DidNotRun" (but not all)
// then return "PartiallyCompleted"
if (allStatuses.has("Failed") || allStatuses.has("Canceled") || allStatuses.has("DidNotRun")) {
return "PartiallyCompleted";
}
// otherwise, return "Completed"
return "Completed";
}
添加回答
举报