91

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.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
Ameen
  • 1,857
  • 2
  • 23
  • 30

11 Answers11

161

Taking a column is easy with the map function.

// a two-dimensional array
var two_d = [[1,2,3],[4,5,6],[7,8,9]];

// take the third column
var col3 = two_d.map(function(value,index) { return value[2]; });

Why bother with the slice at all? Just filter the matrix to find the rows of interest.

var interesting = two_d.filter(function(value,index) {return value[1]==5;});
// interesting is now [[4,5,6]]

Sadly, filter and map are not natively available on IE9 and lower. The MDN documentation provides implementations for browsers without native support.

Leif Carlsen
  • 3,787
  • 3
  • 17
  • 10
67

Use Array.prototype.map() with an arrow function:

const arrayColumn = (arr, n) => arr.map(x => x[n]);

const twoDimensionalArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
];

console.log(arrayColumn(twoDimensionalArray, 0));

Note: Array.prototype.map() and arrow functions are part of ECMAScript 6 and not supported everywhere, see ECMAScript 6 compatibility table.

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
  • 3
    Is it just me, or is this unnecessarily complicated in js? At any rate, that worked, thanks @Michal. – James Feb 25 '18 at 04:08
25

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
Reisepass
  • 613
  • 6
  • 6
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 8
    I knew that i can do it this way, but i thought there was a predefined function or method that retrieves this data. Anyways, thanks for the answer appreciate it. – Ameen Oct 21 '11 at 14:19
20
var data = [
    ["a1", "a2", "a3"],
    ["b1", "b2", "b3"],
    ["c1", "c2", "c3"]
];

var col0 = data.map(d => d[0]); // [ 'a1', 'b1', 'c1' ]

var col1 = data.map(d => d[1]); // [ 'a2', 'b2', 'c2' ]
Yuri Khristich
  • 13,448
  • 2
  • 8
  • 23
15

You can use the following array methods to obtain a column from a 2D array:

Array.prototype.map()

const array_column = (array, column) => array.map(e => e[column]);

Array.prototype.reduce()

const array_column = (array, column) => array.reduce((a, c) => {
  a.push(c[column]);
  return a;
}, []);

Array.prototype.forEach()

const array_column = (array, column) => {
  const result = [];

  array.forEach(e => {
    result.push(e[column]);
  });

  return result;
};

If your 2D array is a square (the same number of columns for each row), you can use the following method:

Array.prototype.flat() / .filter()

const array_column = (array, column) => array.flat().filter((e, i) => i % array.length === column);
Grant Miller
  • 27,532
  • 16
  • 147
  • 165
10

ES6 Version in Javascript:

Using Object keys:

var haystack = [
 {a:1, b:2},
 {a:3, b:4},
 {a:5, b:6}
];

var b_col = haystack.map(x => x.b); // [2,4,6]

Using nested array indexes:

var haystack2 = [
  [1,2,3,4,5],
  [5,4,3,2,1],
  [9,8,7,6,5],
  [5,6,7,8,9]
];

var col_2 = haystack.map(x => x[2]); // [3,3,7,7]

@Pylon's answer is also a good way to add it to the Array prototype.

Brian Berneker
  • 571
  • 6
  • 6
5

function arrayColumn(arr, n) {
  return arr.map(x=> x[n]);
}

var twoDimensionalArray = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

console.log(arrayColumn(twoDimensionalArray, 1));
3

This function works to arrays and objects. obs: it works like array_column php function. It means that an optional third parameter can be passed to define what column will correspond to the indices of return.

function array_column(list, column, indice){
    var result;

    if(typeof indice != "undefined"){
        result = {};

        for(key in list)
            result[list[key][indice]] = list[key][column];
    }else{
        result = [];

        for(key in list)
            result.push( list[key][column] );
    }

    return result;
}

This is a conditional version:

function array_column_conditional(list, column, indice){
    var result;

    if(typeof indice != "undefined"){
        result = {};

        for(key in list)
            if(typeof list[key][column] !== 'undefined' && typeof list[key][indice] !== 'undefined')
                result[list[key][indice]] = list[key][column];
    }else{
        result = [];

        for(key in list)
            if(typeof list[key][column] !== 'undefined')
                result.push( list[key][column] );
    }

    return result;
}

usability:

var lista = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];

var obj_list = [
  {a: 1, b: 2, c: 3},
  {a: 4, b: 5, c: 6},
  {a: 8, c: 9}
];

var objeto = {
  d: {a: 1, b: 3},
  e: {a: 4, b: 5, c: 6},
  f: {a: 7, b: 8, c: 9}
};

var list_obj = {
  d: [1, 2, 3],
  e: [4, 5],
  f: [7, 8, 9]
};

console.log( "column list: ", array_column(lista, 1) );
console.log( "column obj_list: ", array_column(obj_list, 'b', 'c') );
console.log( "column objeto: ", array_column(objeto, 'c') );
console.log( "column list_obj: ", array_column(list_obj, 0, 0) );

console.log( "column list conditional: ", array_column_conditional(lista, 1) );
console.log( "column obj_list conditional: ", array_column_conditional(obj_list, 'b', 'c') );
console.log( "column objeto conditional: ", array_column_conditional(objeto, 'c') );
console.log( "column list_obj conditional: ", array_column_conditional(list_obj, 0, 0) );

Output:

/*
column list:  Array [ 2, 5, 8 ]
column obj_list:  Object { 3: 2, 6: 5, 9: undefined }
column objeto:  Array [ undefined, 6, 9 ]
column list_obj:  Object { 1: 1, 4: 4, 7: 7 }

column list conditional:  Array [ 2, 5, 8 ]
column obj_list conditional:  Object { 3: 2, 6: 5 }
column objeto conditional:  Array [ 6, 9 ]
column list_obj conditional:  Object { 1: 1, 4: 4, 7: 7 }
*/
Doglas
  • 642
  • 1
  • 11
  • 22
3

I have created a library matrix-slicer to manipulate with matrix items. So your problem could be solved like this:

var m = new Matrix([
    [1, 2],
    [3, 4],
]);

m.getColumn(1); // => [2, 4]

Possible it will be useful for somebody. ;-)

Alexandr
  • 522
  • 2
  • 10
2

Just like the previous posts, you can write a function to archive. But we could add the function over the Array.prototype as below:

Array.prototype.column = function(i) {
  try { 
    return this.map( x => x[i]);
  } catch (e) {
    // catch error: out of index or null array ....
    console.log(e);
  }
}

let array =
[[`animal`,`carnivours`],
 [`cow`,`milk`],
 [`plant`,`small`],
 [`fish`,`tank`]];
  
console.log(array.column(0))
console.log(array.column(1))

I think it's definitely more elegent.

Pylon
  • 698
  • 6
  • 9
0

There is also a way involved using .bind().

function getColumn(twoDArr,columnIndex){
    function getCol(value){
       return [value[columnIndex]]
    } 
    return twoDArr.map(getCol.bind(columnIndex));
}