0

I need to parse and display solr facets which are returned in either JSON or Ruby formar:

Collections: [ "a", 1, "b", 2, "c", 3, "d", 4, ... ]

into

{"a"=>1, "b"=>2, "c"=>3, "d"=>4} 

What is the cleanest way?

2 Answers 2

2

EDIT: Well now that we know what you actually want, a hash ...

collections = ["a", 1, "b", 2, "c", 3, "d", 4]
Hash[*collections]
# => {"a"=>1, "b"=>2, "c"=>3, "d"=>4} 

Original answer: I may not understand your goal but...

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"]
Sign up to request clarification or add additional context in comments.

Comments

1

What i see you want to do is maybe a hash ? {a => "1", b => "2"} ??

If so, read below:

collections = [ "a", 1, "b", 2, "c", 3, "d", 4]


result = Hash[*collections.flatten]

result prints {"a"=>1, "b"=>2, "c"=>3, "d"=>4} 

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.