Ricky Spanish Ricky Spanish - 9 days ago 7
Ruby Question

ruby convert multidimensional array into one array

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.

Answer

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]]