我有一个数组:
a = [[676, "/"], [679, "/", 680, "/"], [4, "3.7", "111.55", "/"], [668, "/"], [681, "/", 682, "/"]]
想要这样的输出:
a = [[676], [679], [680], [4, "3.7", "111.55"], [668], [681], [682]]
尝试过这样的事情:
a.flatten.join(",").split("/")
它给出了这样的东西:
["676,", ",679,", ",680,", ",4,3.7,111.55,", ",668,", ",681,", ",682,"]
最佳答案
这是你需要的:
a.flatten.chunk { |x| x != '/' }.select(&:first).map(&:last)
#=> [[676], [679], [680], [4, "3.7", "111.55"], [668], [681], [682]]
或者:
a.flatten.chunk { |x| x != '/' }.map { |bool, arr| arr if bool }.compact
关于ruby-on-rails - 用斜杠将 Ruby 数组拆分为子数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11895939/