Gian Lorenzo Abaño Gian Lorenzo Abaño - 4 months ago 8
Javascript Question

splice on for loop and change the index number of the activity

i have a variable like this:

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":[]},
]
};


and i have the code to delete a specific activity here:

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());
}


The problem is the task is connected to the activities variable by an index. When you delete a specific activity in the "activities" variable the task activity index more than the activity deleted index needs to be decreased by 1 to be arranged to the activities variable again but my code doesn't seem to work. How can i fix this code?

Answer

It's the for loop where you decrease the activity. If i am reading the code right the problem is that after you press a button every activity after that one is deleted.

The solution is to do another for loop after you are done deleting items, and decrease the activity there. You also need to lower d by 1 with a d--

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"]--;
  }
}

Comments