循环嵌套条件被数据覆盖

时间:2021-05-14 11:57:50

标签: javascript loops

我有一个包含以下对象的 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"
    }
}

1 个答案:

答案 0 :(得分:1)

我刚刚重写了它......试图调试这是疯狂的......

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;
    }
});
相关问题