我有一个数组:
arr = [["Foo1","Bar1","1","W"],["Foo2","Bar2","2","X"],["Foo3","Bar3","3","Y"],["Foo4","Bar4","4","Z"]]
而且我想要一个仅包含每个数组的第三列的数组:
res = ["1","4"]
我该怎么办?
我想输入:
arr[][2]
但是想像Ruby一样,我试过:
arr.select{ |r| r[2] }
但这返回整行.
解决方法
你想要arr.map {| row |行[2]}
arr = [["Foo1","Z"]] arr.map {|row| row[2]} # => ["1","4"]