Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个结构数组[a, [b], c, [d], ...],例如:
[a, [b], c, [d], ...]
array = [0, [1], 2, [1]]
我需要:
[[0, 1], [2, 1]]
我该怎么做?:P
更新:
我想知道如何处理这个数组
array = [0, [], 1, [], 2, []]
进入
那就是删除空的并相应地合并,如上所示。
谢谢 :)
我会写:
array.each_slice(2).map { |x, ys| [x, ys.first] } #=> [[0, 1], [2, 1]]
请注意,您也可以编写,map { |x, (y)| [x, y] }尽管它肯定是一个神秘的拆包。
map { |x, (y)| [x, y] }
array.flatten.each_slice(2).to_a