How can I retrieve a column from a 2-dimensional array and not a single entry? I’m doing this because I want to search for a string in one of the columns only so if there is another way to accomplish this please tell me.
I’m using the array defined this way:
var array=[];
At the end the size of this array is 20(col)x3(rows) and I need to read the first row and check the existence of some phrase in it.
Advertisement
Answer
You have to loop through each element in the 2d-array, and get the nth column.
function getCol(matrix, col){ var column = []; for(var i=0; i<matrix.length; i++){ column.push(matrix[i][col]); } return column; } var array = [new Array(20), new Array(20), new Array(20)]; //..your 3x20 array getCol(array, 0); //Get first column