1

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);
0

3 Answers 3

5

Your error is in getLoc != "". This is false for getLoc === 0.

I know js is not a strong-typed language, but you'll be better of if you write with strong-typed equality checks ===

See e.g. this article for more insights on the subject http://www.sitepoint.com/borrowing-techniques-strongly-typed-languages-javascript/ and it's always a good choice to run your code through a static code analyzer (e.g. http://jslint.com or http://jshint.com).

2

Add console.log(getLoc != "") before //Delete from, and you will see printed false when getLoc=0.

Change

console.log("that number was spotted in position - "+getLoc);
//console.log(getLoc != "")
//Delete from
if(getLoc != "" && getLoc != "-1"){
    mainArray.splice(getLoc, 1);  
}

by

console.log("that number was spotted in position - "+getLoc);
//console.log(getLoc !== "")
//Delete from
if(getLoc !== "" && getLoc != "-1"){
    mainArray.splice(getLoc, 1);  

}
4
  • Upvote for telling the OP how to discover the problem on their own. Feb 13, 2016 at 18:25
  • @JeremyJStarcher Do you think is better to show the error than solve the issue? Feb 14, 2016 at 2:55
  • 1
    Both, of course. I was just saying that I like educational answers that, hopefully, teach people how to do their own debugging. Feb 14, 2016 at 4:57
  • I agree @JeremyJStarcher Feb 14, 2016 at 15:22
1

Functional approach might be much more readable in this case:

mainArray = mainArray.filter(arr => arr[0] !== arrayToDelete);
1
  • 2
    This shares the code-fault of the original != vs !== Feb 13, 2016 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.