Deleting a column from a multidimensional array in javascript

前端 未结 6 1297
遥遥无期
遥遥无期 2020-12-06 00:20

I have a 2D array:

var array = [[\"a\", \"b\", \"c\"],[\"a\", \"b\", \"c\"],[\"a\", \"b\", \"c\"]]

I want to delete an entire column of thi

6条回答
  •  南方客
    南方客 (楼主)
    2020-12-06 00:53

    splice is cool. It resizes the array as it removes things so you don't end up with nulls. So using splice you just have to iterate through each row and remove the right element.

    var removeCol = function(arr2d, colIndex) {
        for (var i = 0; i < arr2d.length; i++) {
            var row = arr2d[i];
            row.splice(colIndex, 1);
        }
    }
    

    http://jsfiddle.net/eB8LD/

提交回复
热议问题