EDIT: my mistake. I typed slice instead of splice. Forget I wrote this.
Need to delete elements from an array of objects. But after .splice(index,1), the array stays unchanged. The splice itself works OK: it returns exactly the element which should be deleted.
I remarked-out the
numItemsCounter --;
(which should be there), because if not, the loop is eternal (because the element is never deleted).
Must be doing something wrong, but I have looked everywhere, can´t find it.
BTW, the objMap (the array of objects) is handed down from another function.
Here the code:
function fnKillThisUnitFromArray(objMap, strUnitId) {
let numItemsCounter;
for (numItemsCounter = 0; numItemsCounter < objMap.items.length; numItemsCounter++) {
if (objMap.items[numItemsCounter].UnitId === strUnitId) {
var objDummy = objMap.items.slice(numItemsCounter, 1);
console.log("Deleted from objmap;" + JSON.stringify(objDummy));
console.log("objMap after 1 kill " + strUnitId + "=" + JSON.stringify(objMap));
// numItemsCounter --;
}
}
console.log("objMap after all kills " + strUnitId + "=" + JSON.stringify(objMap));
}
Anyone?