Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top