1
first_response = [
  {"xId" => "123", "yId" => "321"}, 
  {"xId" => "x",   "yId" => "y"  }
]

first_response.each do |resp|
  x_id = resp['xId']
  y_id = resp['yId']
  puts x_id.to_s
  puts y_id.to_s
end                                                              
                                                                      

This gives me outputs

123
321
x
y  
                                                                       

output hash I want to create is {123=>{321}, x=>{y}}

first service: I have an array of hash that has two different ids example:(x_id and y_id) (there would be multiple pairs like that in the response)

I want to create a hash that should contain the matching pair of x_id and y_ids that we get from the first service with x_id's as the key for all the pairs.

4
  • 1
    Do you have examples of the input and expected output, and what you've tried? Commented Dec 4, 2021 at 16:12
  • Hash[[[:a, 1], [:b, 2], [:c, 3]]] gives => {:a=>1, :b=>2, :c=>3} Commented Dec 4, 2021 at 16:19
  • { "first_service": [ { "x": "string", "y": "string", "other": { "a": "string", "b": "string", "c": "string" }, "other-2": { "l": "CONFIRMED", "m": "2021-12-03T15:10:21.330Z", "n": "2021-12-03T15:10:21.330Z" } } ] } I want to group this as hash_ = { "x": { 'y': "y", } } Commented Dec 4, 2021 at 16:38
  • I can iterate through the first service and get the x and y separately in their own array by iterating through the first service one by one like belowfor both (x and y seperately), but not able to group them. # x = [].tap do |ids| # first.each do |element| # x = element['x'] # ids.push(x) unless x.nil? || ids.include?(x) # end # end Commented Dec 4, 2021 at 16:45

2 Answers 2

2

If you know every hash in first_response is going to contain exactly two key/value pairs, you can extract their values and then convert that result into a hash (see Enumerable#to_h):

first_response.to_h(&:values)
# {"123"=>"321", "x"=>"y"}
Sign up to request clarification or add additional context in comments.

2 Comments

so using the above approach that I shared I can get the output but I think I need to add another key inside of the first one and get the output like {"123"=>{"abc" =>321"}, "x"=>{"xyz"=>"y"}}, how can achieve that
Can you update your question? I don't get how you come up with those abc/xyz keys there @Dev.
1

Looks like this approach works, but I am not completely sure if that is right

first_response = [{"xId"=>"123","yId"=> "321"}, {"xId"=>"x","yId"=> "y"}]                         
h = {}.tap do |element|
  first_response.each do |resp|
    x_id = resp['xId']
    y_id = resp['yId']
    element[x_id] = y_id
  end
end
puts h.to_s
# {"123"=>"321", "x"=>"y"}                                      

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.