gpt4 book ai didi

arrays - 我如何遍历子数组的数组

转载 作者:数据小太阳 更新时间:2023-10-29 08:25:54 28 4
gpt4 key购买 nike

我有一个像这样的子数组

[["a", "b", "c"], ["d", "e", "f"], ["g", "h", nil]]

我如何迭代它来创建一个 html 表格,其子数组是表格的一列,如下所示?

a d g
b e h
c f

最佳答案

今天是你的幸运日,ruby 正适合你,Array#transpose .

ary = [["a", "b", "c"], ["d", "e", "f"], ["g", "h", nil]]

ary.transpose.each {|a| p a }
# >> ["a", "d", "g"]
# >> ["b", "e", "h"]
# >> ["c", "f", nil]

关于arrays - 我如何遍历子数组的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51308849/

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