我合并了 2 个列表。
list1 = customer.links.where(ext: true).group(:link_id, :external).limit(100).order('count_id desc').count('id')
list2 = customer.links.where(ext: false).where.not(url: '/specific_link').group(:url, :ext).limit(100).order('count_id desc').count('id')
list = list1.merge(list2).sort_by{|k, v| -v}
结果是:
[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]]
我想把它转换成一个维度哈希,像这样:
[["/path/element1", false, 7], [4, true, 5], ["/path/element6", false, 1]]
当我使用 flatten
时,数组之间没有分隔。
最佳答案
对每个子阵列进行简单易行的展平。
output_array=[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]].map{|x| x.flatten}
关于ruby-on-rails - ruby 将多维数组转换为一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40874674/