从二维数组中获取列

如何从 二维数组检索一列而不是一个条目? 我这样做是因为我想搜索一个字符串在其中的一列,只有这样,如果有其他方法来完成这一点,请告诉我。

我使用这样定义的数组:

var array=[];

最后,这个数组的大小是20(colx3)(行) ,我需要读取第一行并检查其中是否存在某个短语。

200311 次浏览

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

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.

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 }
*/

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.

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));

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. ;-)

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);
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' ]

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.

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.

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

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