gpt4 book ai didi

javascript - 从二维数组中获取列

转载 作者:IT王子 更新时间:2023-10-29 03:00:38 27 4
gpt4 key购买 nike

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

我正在使用这样定义的数组:

var array=[];

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

最佳答案

使用 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]; });

为什么要为切片烦恼呢?就filter找到感兴趣的行的矩阵。

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

可悲的是,filter and map are not natively available on IE9 and lower . MDN 文档为没有 native 支持的浏览器提供实现。

关于javascript - 从二维数组中获取列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7848004/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com