从二维数组获取列 [英] Get column from a two dimensional array

查看:89
本文介绍了从二维数组获取列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从二维数组而不是单个条目中检索列? 我这样做是因为我只想在其中一列中搜索字符串,所以如果有另一种方法可以完成此操作,请告诉我.

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=[];

最后,此数组的大小为20(col)x3(rows),我需要读取第一行并检查其中是否存在某些短语.

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.

推荐答案

您必须遍历2d数组中的每个元素,并获得第 n 列.

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

这篇关于从二维数组获取列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆