javascript - 循环嵌套条件被数据覆盖
问题描述
我有一个 JSON 文件,其中包含以下对象:
{
"id": 23,
"active": true,
"state": "on",
"dependency": [
{
"id": 24,
"type": "critical"
},
{
"id": 30,
"type": "moderate"
},
{
"id": 25,
"type": "critical"
},
{
"id": 35,
"type": "moderate"
}
]
},
依赖值是无序的,将从其他函数中填充。此订单未排序。此外,依赖项的类型“选择”根 SVG 对象颜色。严重的将是红色[错误],中等的将是黄色[有限]。
如果所有“中等”条目都停留在底部,则循环有效。一旦列表混合在一起,就像上面的例子一样。状态永远不会“受限”,因为它会被“错误”覆盖。
我创建了一个证明布尔值,它应该避免这种情况,但每个 console.log 都能更好地教我。任何想法,我几乎是盲目的。
for (var i = 0; i < graph.nodes.length; i++) {
isModerate = false
if (graph.nodes[i].active === true && graph.nodes[i].dependency.length == 0) {
graph.nodes[i].state = "on"
} else if (graph.nodes[i].active === true && graph.nodes[i].dependency.length > 0) {
for (var j = 0; j < graph.nodes[i].dependency.length; j++) {
if (graph.nodes[i].dependency[j].type === "critical") {
if (!isModerate && graph.nodes[graph.nodes[i].dependency[j].id].state === "on") {
graph.nodes[i].state = "on"
} else if (graph.nodes[graph.nodes[i].dependency[j].id].state === "limited" || "error" || "off") {
graph.nodes[i].state = "error"
break
}
} else if (graph.nodes[i].dependency[j].type === "moderate") {
if (graph.nodes[graph.nodes[i].dependency[j].id].state === "on" ) {
graph.nodes[i].state = "on"
} else if (graph.nodes[graph.nodes[i].dependency[j].id].state === "limited" || "error" || "off" ) {
graph.nodes[i].state = "limited"
isModerate = true
}
}
}
} else if (graph.nodes[i].active === false) {
graph.nodes[i].state = "off"
}
}
解决方案
我只是重写了它...尝试调试这太疯狂了...
for (var i = 0; i < graph.nodes.length; i++) {
isModerate = false
if (graph.nodes[i].active === true && graph.nodes[i].dependency.length == 0) {
graph.nodes[i].state = "on"
} else if (graph.nodes[i].active === true && graph.nodes[i].dependency.length > 0) {
// ------------------------------
// item in dependency lsit
for (var j = 0; j < graph.nodes[i].dependency.length; j++) {
if (graph.nodes[i].dependency[j].type === "critical") {
if (!isModerate && graph.nodes[graph.nodes[i].dependency[j].id].state === "on") {
graph.nodes[i].state = "on"
} else if (graph.nodes[graph.nodes[i].dependency[j].id].state === "limited" || "error" || "off") {
// ------------------------------
// you break at here
// so any remain items in the dependency will be skip/ignore
graph.nodes[i].state = "error"
break
// ------------------------------
}
} else if (graph.nodes[i].dependency[j].type === "moderate") {
if (graph.nodes[graph.nodes[i].dependency[j].id].state === "on") {
graph.nodes[i].state = "on"
} else if (graph.nodes[graph.nodes[i].dependency[j].id].state === "limited" || "error" || "off") {
graph.nodes[i].state = "limited"
isModerate = true
}
}
}
// ------------------------------
} else if (graph.nodes[i].active === false) {
graph.nodes[i].state = "off"
}
}
我只看到 4 个状态,所以无论你想用这个做什么
graph.nodes[graph.nodes[i].dependency[j].id].state === "limited" || "error" || "off"
和这个一样
graph.nodes[graph.nodes[i].dependency[j].id].state !== "on"
我最好猜测你的代码想要做什么并重写它......
// I guess this is the structure...
let graph = {
nodes: [
{
"id": 23,
"active": true,
"state": "on",
"dependency": [
{ "id": 24, "type": "critical" },
{ "id": 30, "type": "moderate" },
{ "id": 25, "type": "critical" },
{ "id": 35, "type": "moderate" }
]
},
],
};
const
// use dictionary, so IDE can:
// auto highlight/change it all/auto snippet for you & avoid typo
// easier to track/change, try to replace string is messy
state = {
on: "on",
limited: "limited",
error: "error",
off: "off",
},
type = {
critical: "critical",
moderate: "moderate",
},
// a function to get specific node's state
getNodeState = (id) => {
let node = graph.nodes.find(node => node.id == id);
if (!node) {
throw Error(`node id:${id} not found`);
} else {
return node.state;
}
};
graph.nodes.forEach(node => {
let isModerate = false;
if (node.active) {
if (node.dependency.length === 0) {
node.state = state.on;
} else {
node.dependency.forEach(dependency => {
if (dependency.type == type.critical) {
if (!isModerate && (getNodeState(dependency.id) == state.on)) {
node.state = state.on;
} else if (node.state != state.on) {
// I only see 4 states, just revers it
node.state = state.error;
// no break here, it will continue to process all items in the dependency
}
} else if (dependency.type == type.moderate) {
if (getNodeState(dependency.id) == state.on) {
node.state = state.on;
} else if (node.state != state.on) {
node.state = state.limited;
isModerate = true;
}
}
});
}
} else {
node.state = state.off;
}
});
推荐阅读
- json - TSQL:JSON 路径中的递归下降
- angular - 角度文件多次上传,没有重复
- android - 无法解析“CellIdentityLte”中的方法“getEarfcn”
- casting - 在 YouTube 上获取 Cast 按钮以获取自定义投射解决方案
- sql - 从 sql select 返回过滤结果
- java - 如何使用本机查询在 JPA 中执行嵌套联接
- swift - 截断日期的时间会保留一些时间分量
- java - 辅助功能服务 - performGlobalAction() 不起作用?
- c# - 不使用 Canvas 的 Unity 2D 拖放
- javascript - 如何从括号内百分比的字符串中提取数字