我需要解析并显示以JSON或Ruby formar返回的solr facet:
收藏:[“a”,1,“b”,2,“c”,3,“d”,4,...]
到
{"a"=>1, "b"=>2, "c"=>3, "d"=>4}
最干净的方法是什么?
答案 0 :(得分:2)
编辑:现在我们知道你真正想要的是什么,hash
......
collections = ["a", 1, "b", 2, "c", 3, "d", 4]
Hash[*collections]
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>4}
原始回答:我可能不理解你的目标,但......
collections = ["a", 1, "b", 2, "c", 3, "d", 4]
collections.each_slice(2).map{ |(x, y)| "#{x} - #{y}" }
# => ["a - 1", "b - 2", "c - 3", "d - 4"]
答案 1 :(得分:1)
我认为你想做的可能是哈希? {a => “1”,b => “2”} ??
如果是,请阅读以下内容:
collections = [ "a", 1, "b", 2, "c", 3, "d", 4]
result = Hash[*collections.flatten]
result prints {"a"=>1, "b"=>2, "c"=>3, "d"=>4}