从在JavaScript二维阵列获得列 [英] Get column from a two-dimensional array in JavaScript

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

问题描述

我如何可以检索从二维数组列?

How can I retrieve a column from a two-dimensional array?

这阵有20列和3行,我想要得到的第一行。

This array has 20 columns and 3 rows and I want to get the the first row.

推荐答案

您必须通过二维数组中每个元素循环,并获得的 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

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

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