1

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.

3 Answers 3

8

Easy and simple do flatten to each sub array.

output_array=[[["/path/element1", false], 7], [[4, true], 5], [["/path/element6", false], 1]].map{|x| x.flatten}
Sign up to request clarification or add additional context in comments.

Comments

4

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

5 Comments

@@sagarpandya82 beat me to it.
No, we have different answers!
And this is te perfect answer for my problem. Thank you
@RickySpanish updated with a possibly better way, take a look.
@RickySpanish removed alternative, as Anuj has already posted, it's actually the better solution so I'd accept that answer.
3

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.