I currently have an input [['a', [0, 1]], ['b', [1]]]
. I'm trying to combine the first item to every element in [0,1]
i.e.: 'a'
in ['a',[0,1] => [['a',0],['a',1],['b',1]]
like ordered pairs. I've done it but it seems overly complicated I thought there might be a method I've overlooked.
[[0, [0, 1]], [1, [1]]].map.with_index{|x,y| x[1].map{|ele| [y,ele]}}.flatten(1)
#I used 'a'&'b' in the example to help with any confusion.
▶ arr.flat_map { |e| [e.first].product(e.last) }
#⇒ [["a", 0], ["a", 1], ["b", 1]]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments