下面的代码目前将#startyear 的副本推送到#new。我需要将其转换为 1 个单个数组,有什么想法吗? 论坛内容不多
startyear = [["a", "b", "z"], ["c", "d"], ["e", "f"], ["g", "h", "i", "j"]]
new = []
startyear.each do |n| #.transpose here?
puts "looping #{n}"
new.push(n)
#n.join is needed somewhere
puts "#{startyear.length} is the length of startyear"
break if startyear.length == startyear.length[4]
end
puts "your new array is : #{new}"
最佳答案
你可以使用 Array#flatten:
startyear = [["a", "b", "z"], ["c", "d"], ["e", "f"], ["g", "h", "i", "j"]]
flattened = startyear.flatten
# flattened is now ["a", "b", "z", "c", "d", "e", "f", "g", "h", "i", "j"]
关于arrays - 将二维数组转换为一维 (Ruby),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36193068/