1

Below I have a simple two dimensional array. If I want to remove the whole sub-array where index 0 of the sub array is equal to Susie, how would I do this?

var array1 = [["Ben", 17, 1], ["Susie", 12, 0], ["Thomas", 21, 1]];

Thanks

2 Answers 2

1

You can traverse through the array and find the index and then do a splice.

var array1 = [["Ben", 17, 1], ["Susie", 12, 0], ["Thomas", 21, 1]];
console.log(array1);

for(var x=0; x<array1.length; x++) {

    if(array1[x][0] == "Susie")
        array1.splice(x,1);

}

console.log(array1);

here is a fiddle.

Sign up to request clarification or add additional context in comments.

2 Comments

Although both answers do the job, I prefer this answer as I can understand exactly what it is doing. Thanks!
just in case you're interested: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… a short reference on the 'filter' method.
1
var initialArray = [["Ben", 17, 1], ["Susie", 12, 0], ["Thomas", 21, 1]];
var subArray = initialArray.filter(function(item) { return item[0] != 'Susie'; });

fiddle: http://jsfiddle.net/XHEJt/1/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.