I have been trying to delete specific parts of an array, and I thought I had it working. But I cannot figure out why I cannot delete the first record.
If I set arrayToDelete to 67(representing the first element of 2nd array), or 9(representing the first element of 3rd array)...it works as I expected. But if I change arrayToDelete to 7(representing the first element of 1st array), it doesn't remove that one.
Where am I going wrong?
var mainArray = [];
var a = [7,"red","Q","gtgtg"];
mainArray.push(a);
var b = [67,"blue","s","ghft"];
mainArray.push(b);
var c = [9,"green","t","rtryt"];
mainArray.push(c);
var pickle = JSON.stringify(mainArray);
console.log("starting array - "+pickle);
console.log("starting array length - "+mainArray.length);
var arrayToDelete = "67";
var getLoc = "";
// Get position of array where first value is "1"
for (var i = 0; i < mainArray.length; i++) {
//console.log(mainArray[i][0]);
if(mainArray[i][0] == arrayToDelete){
//console.log("array number "+i);
getLoc = i;
}
}
console.log("that number was spotted in position - "+getLoc);
//Delete from
if(getLoc != "" && getLoc != "-1"){
mainArray.splice(getLoc, 1);
}
var pickle2 = JSON.stringify(mainArray);
console.log("Array after removal - "+pickle2);
console.log("ending array length - "+mainArray.length);