I've merged 2 lists.
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}
The result is:
[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]]
I want to convert it into a on dimensional hash, like that:
[["/path/element1", false, 7], [4, true, 5], ["/path/element6", false, 1]]
When I use flatten
, there is no separation between the arrays.
轻松简单地将每个子数组展平。
output_array=[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]].map{|x| x.flatten}
You could do something like this:
arr = [[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]]
arr.map { |k,v| [*k,v] }
#=> [["/path/element1", false, 7], [4, true, 5], ["/path/element6", false, 1]]
You can use the Ruby flatten method .
[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]].flatten(1)
result => [["/path/element1", false], 7, [4, true], 5, ["/path/element6", false], 1]
Also posted here in this question/answer .
Update below to at least make this correct (like the other answers). To flatten a level deeper, you can use .map to flatten the lower arrays.
[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]].map {|x| x.flatten }
result => [["/path/element1", false, 7], [4, true, 5], ["/path/element6", false, 1]]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.