拼接for循环并更改活动的索引号

时间:2016-07-10 14:45:22

标签: javascript html

我有一个这样的变量:

deftemplate = {
    "activities":["Earthworks", "Concrete Works", "Steel Works", "Framework", "MansoryWorks", "Carpentry Works", "Roofing Works", "Pre-Fabricated Works", "Painting Works", "Plumbing", "Electrical Works"],
    "tasks":[
    // Earthworks
        {"taskname":"Clearing and Grubbing", "activity":0, "parent":null, "notifyclient":false, "days":5, "manpower":[[1, 0, 0, 1], [6, 0, 0, 2]], "materials":[[1, 0, 0, 3]], "equipments":[]},
        {"taskname":"Excavation", "activity":0, "parent":0, "notifyclient":false, "days":12, "manpower":4, "materials":[[1, 0, 0, 1], [6, 0, 0, 3]], "equipments":[]},
        {"taskname":"Soil Poisoning", "activity":0, "parent":1, "notifyclient":false, "days":5, "manpower":[[6, 0, 0, 1]], "materials":[], "equipments":[]},
        {"taskname":"Gravel Bedding", "activity":0, "parent":2, "notifyclient":false, "days":5, "manpower":[[6, 0, 0, 2]], "materials":[], "equipments":[]},

    // Concrete Works
        {"taskname":"Footings", "activity":1, "parent":3, "notifyclient":false, "days":7, "manpower":[[2, 0, 0, 1], [6, 0, 0, 3], [9, 0, 0, 1]], "materials":[[2, 0, 0, 49], [4, 0, 0, 2], [5, 0, 0, 4]], "equipments":[[3, 0, 0, 1]]},
        {"taskname":"Columns", "activity":1, "parent":4, "notifyclient":false, "days":15, "manpower":[[3, 0, 0, 3], [2, 0, 0, 3], [6, 0, 0, 5]], "materials":[[2, 0, 0, 75], [4, 0, 0, 5], [5, 0, 0, 10]], "equipments":[]},
        {"taskname":"Beams", "activity":1, "parent":5, "notifyclient":false, "days":11, "manpower":[[3, 0, 0, 3], [2, 0, 0, 3], [6, 0, 0, 5]], "materials":[[2, 0, 0, 55], [4, 0, 0, 4.5], [5, 0, 0, 9]], "equipments":[]},
        {"taskname":"Slabs", "activity":1, "parent":6, "notifyclient":false, "days":8, "manpower":[[2, 0, 0, 2], [3, 0, 0, 2], [6, 0, 0, 4]], "materials":[[2, 0, 0, 52], [4, 0, 0, 4.5], [5, 0, 0, 9]], "equipments":[]},

    // Steel Works
        {"taskname":"Footings", "activity":2, "parent":0, "notifyclient":false, "days":4, "manpower":[[11, 0, 0, 1], [6, 0, 0, 1]], "materials":[[6, 0, 0, 25]], "equipments":[]},
        {"taskname":"Columns", "activity":2, "parent":0, "notifyclient":false, "days":22, "manpower":[[7, 0, 0, 2], [6, 0, 0, 1]], "materials":[[6, 0, 0, 68], [7, 0, 0, 145], [8, 0, 0, 285]], "equipments":[]},
        {"taskname":"Beams", "activity":2, "parent":0, "notifyclient":false, "days":19, "manpower":[[7, 0, 0, 2], [6, 0, 0, 1]], "materials":[[6, 0, 0, 57], [7, 0, 0, 105], [8, 0, 0,150]], "equipments":[]},
        {"taskname":"Slabs", "activity":2, "parent":0, "notifyclient":false, "days":10, "manpower":[[7, 0, 0, 2], [6, 0, 0, 1]], "materials":[[8, 0, 0, 165]], "equipments":[]},

    // Framework
        {"taskname":"Formworks and Scaffoldings", "activity":3, "parent":0, "notifyclient":false, "days":35, "manpower":[[6, 0, 0, 3], [3, 0, 0, 5]], "materials":[[9, 0, 0, 1050], [10, 0, 0, 75]], "equipments":[]},

    // Mansoryworks
        {"taskname":"Masonry", "activity":4, "parent":0, "notifyclient":false, "days":41, "manpower":[[2, 0, 0, 5], [6, 0, 0, 3]], "materials":[[11, 0, 0, 1042], [12, 0, 0, 469]], "equipments":[]},

    // Carpentry Works
        {"taskname":"Ceilings, Cabinets and Etc.", "activity":5, "parent":13, "notifyclient":false, "days":31, "manpower":[[7, 0, 0, 2], [6, 0, 0, 1]], "materials":[[10, 0, 0, 56], [13, 0, 0, 235]], "equipments":[]},

    // Roofing Works
        {"taskname":"Roofing Materials, Trusses and etc", "activity":6, "parent":14, "notifyclient":false, "days":12, "manpower":[[3, 0, 0, 5], [6, 0, 0, 3]], "materials":[[14, 0, 0, 17], [15, 0, 0, 1292], [16, 0, 0, 138]], "equipments":[]},

    // Pre-Fabriacted Works
        {"taskname":"Doors", "activity":7, "parent":15, "notifyclient":false, "days":9, "manpower":[[3, 0, 0, 2], [6, 0, 0, 2]], "materials":[[17, 0, 0, 2]], "equipments":[]},
        {"taskname":"Windows", "activity":7, "parent":16, "notifyclient":false, "days":9, "manpower":[[3, 0, 0, 2], [6, 0, 0, 2]], "materials":[[17, 0, 0, 14]], "equipments":[]},

    // Painting Works
        {"taskname":"Exterior", "activity":8, "parent":17, "notifyclient":false, "days":24, "manpower":[[8, 0, 0, 4]], "materials":[[18, 0, 0, 10], [19, 0, 0, 3], [20, 0, 0, 1], [21, 0, 0, 5], [22, 0, 0, 3]], "equipments":[]},

    // Plumbing ---
        {"taskname":"Fixtures", "activity":9, "parent":0, "notifyclient":false, "days":2, "manpower":[[5, 0, 0, 2], [6, 0, 0, 1]], "materials":[[23, 0, 0, 1], [24, 0, 0, 22], [25, 0, 0, 22], [26, 0, 0, 22]], "equipments":[]},
        {"taskname":"Waterlines", "activity":9, "parent":0, "notifyclient":false, "days":12, "manpower":[[5, 0, 0, 1], [6, 0, 0, 1]], "materials":[[27, 0, 0, 5], [28, 0, 0, 20], [29, 0, 0, 3]], "equipments":[]},
        {"taskname":"Sanitary Sewerline", "activity":9, "parent":0, "notifyclient":false, "days":12, "manpower":[[5, 0, 0, 1], [6, 0, 0, 1]], "materials":[[30, 0, 0, 4], [31, 0, 0, 3], [32, 0, 0, 2]], "equipments":[]},
        {"taskname":"Storm Drain", "activity":9, "parent":0, "notifyclient":false, "days":12, "manpower":[[5, 0, 0, 1], [6, 0, 0, 1]], "materials":[[31, 0, 0, 15]], "equipments":[]},

    // Electrical Works ---
        {"taskname":"Fixtures and Boxes", "activity":10, "parent":0, "notifyclient":false, "days":3, "manpower":[[4, 0, 0, 1], [6, 0, 0, 1]], "materials":[[33, 0, 0, 3], [34, 0, 0, 1]], "equipments":[]},
        {"taskname":"Fittings", "activity":10, "parent":0, "notifyclient":false, "days":5, "manpower":[[4, 0, 0, 2], [6, 0, 0, 2]], "materials":[[35, 0, 0, 100]], "equipments":[]},
        {"taskname":"Wiring", "activity":10, "parent":0, "notifyclient":false, "days":10, "manpower":[[4, 0, 0, 2], [6, 0, 0, 2]], "materials":[[36, 0, 0, 5]], "equipments":[]},
        {"taskname":"Enclosed Circuit Breaker", "activity":10, "parent":0, "notifyclient":false, "days":2, "manpower":[[4, 0, 0, 1]], "materials":[[37, 0, 0, 8]], "equipments":[]},
        {"taskname":"Testing and Commissioning", "activity":10, "parent":0, "notifyclient":false, "days":2, "manpower":[[4, 0, 0, 1], [6, 0, 0, 1]], "materials":[], "equipments":[]},
    ]
};

我在这里有删除特定活动的代码:

function delact(event) {
var curact = parseInt(event.target.getAttribute("curact"));
deftemplate["activities"].splice(curact, 1);
for (var d = 0; d < deftemplate["tasks"].length; d++) {
    if (deftemplate["tasks"][d]["activity"] == curact) {
        for (var g = 0; g < deftemplate["tasks"][d]["equipments"].length; g++) {
            restoreCount2(deftemplate["tasks"][d]["equipments"][g][0], deftemplate["tasks"][d]["equipments"][g][1]);
        }

        for (var g = 0; g < deftemplate["tasks"][d]["manpower"].length; g++) {
            restoremCount2(deftemplate["tasks"][d]["manpower"][g][0], deftemplate["tasks"][d]["manpower"][g][1]);
        }

        for (var y = 0; y < deftemplate["tasks"].length; y++) {
            if (deftemplate["tasks"][y]["parent"] == d) {
                deftemplate["tasks"][y]["parent"] = null;
            }
            if (deftemplate["tasks"][y]["activity"] > curact) {
                console.log("Task Name: " + deftemplate["tasks"][y]["taskname"]);
                deftemplate["tasks"][y]["activity"]--;
            }
        }
        deftemplate["tasks"].splice(d, 1);
    }
}
loadacts($("#prjstartdate").val());
}

问题是任务通过索引连接到活动变量。当您删除&#34;活动&#34;中的特定活动时变量任务活动索引多于活动删除索引需要减少1再次安排到活动变量但我的代码似乎没有工作。我该如何修复此代码?

3 个答案:

答案 0 :(得分:0)

问题在于这一行

for

for循环。删除项目时,下一个项目位于同一个插槽中。然后d递增索引,然后跳过索引。

结论:拼接后减少deftemplate["tasks"].splice(d, 1); d--;

mmap failed: ENOMEM (Out of memory)

(我建议使用更好的数据结构。)

答案 1 :(得分:0)

它是你减少活动的for循环。如果我正在阅读正确的代码,问题是在按下按钮之后,每个活动都被删除。

解决方法是在完成删除项目后再执行另一个for循环,并减少那里的活动。您还需要使用d -

将d降低1

&#13;
&#13;
var curact = parseInt(event.target.getAttribute("curact"));
deftemplate["activities"].splice(curact, 1);
for (var d = 0; d < deftemplate["tasks"].length; d++) {
    if (deftemplate["tasks"][d]["activity"] == curact) {
        for (var g = 0; g < deftemplate["tasks"][d]["equipments"].length; g++) {
            restoreCount2(deftemplate["tasks"][d]["equipments"][g][0], deftemplate["tasks"][d]["equipments"][g][1]);
        }

        for (var g = 0; g < deftemplate["tasks"][d]["manpower"].length; g++) {
            restoremCount2(deftemplate["tasks"][d]["manpower"][g][0], deftemplate["tasks"][d]["manpower"][g][1]);
        }

        for (var y = 0; y < deftemplate["tasks"].length; y++) {
            if (deftemplate["tasks"][y]["parent"] == d) {
                deftemplate["tasks"][y]["parent"] = null;
            }
        deftemplate["tasks"].splice(d, 1);
        d--;
    }
}
for(var y = 0; y < deftemplate["tasks"].length; y++){
  if (deftemplate["tasks"][y]["activity"] > curact) {
    console.log("Task Name: " + deftemplate["tasks"][y]["taskname"]);
    deftemplate["tasks"][y]["activity"]--;
  }
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

如果要从数组中删除项目,则应该向后迭代。

for (var d = deftemplate["tasks"].length -1; d >= 0; d--) {
    // deftemplate["tasks"][d] can be removed...
}