1

I am trying to call the uniq method on the follow json so that it would only return unique result base on employee_id

# Json array
a ={
  results: [
   {
    employee: {
      name: "A",
      employee_id: "A-00016",
      title: 1
     }
   },{
    employee: {
      name: "A",
      employee_id: "A-00016",
      title: 2
     }
   },{
    employee: {
      name: "C",
      employee_id: "C-00017",
      title: 3
     }
    }
   ]
  }



# Calling uniq on a
a.uniq { |p| p.values_at(:employee_id) }

However, I am only getting this result

{
  results: [
   {
    employee: {
      name: "A",
      employee_id: "A-00016",
      title: 1
     }
    }
   ]
  }

Instead of what I want

{
  results: [
   {
    employee: {
      name: "A",
      employee_id: "A-00016",
      title: 1
     },{
    employee: {
      name: "C",
      employee_id: "C-00017",
      title: 3
     }
    }
   ]
  }

Am I using the correct method to output the result I want?

1
  • I would recommend to do : a[:results].uniq! but you have title=1 in the first instance and title=2 in the second. So, ruby will consider the two first as different. Commented Jun 28, 2016 at 14:23

3 Answers 3

3

With uniq:

input[:results].uniq { |e| e[:employee][:employee_id] }
#⇒ [
#    {:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>"1"}},
#    {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>"3"}}]

But I believe there should be some condition applied on what to choose from siblings having the same id. The code below selects the one, having max title value:

input[:results].group_by { |e| e[:employee][:employee_id] }
               .map { |_, v| v.max_by { |e| e[:employee][:title].to_i } }
#⇒ [
#    {:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>"2"}},
#    {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>"3"}}]
Sign up to request clarification or add additional context in comments.

Comments

2

Here is one way to do this, in order to return the modified input hash, we can use uniq! which will modify the array a[:results] in place. We use dup to duplicate the hash a to preserve it, and then use tap to operate on duplicated hash.

r = a.dup.tap do |h|
  h[:results].uniq! do |h|
    h[:employee][:employee_id]
  end
end

#=> {:results=>
#    [
#     {:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>1}},
#     {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}
#    ]
#   }

2 Comments

Thank you! The method works but I voted @mudasobwa answer because it uses uniq.
@TomCheung Other answer does not produce the desired output. This solution uses uniq! to get the desired output
1
def selection_criterion(h)
  h[:title].to_i
end

{results: a[:results].group_by {|h| h[:employee][:employee_id]}.
                      values.
                      map {|arr| arr.max_by {|h|  selection_criterion(h[:employee])}}}
   #=> {:results=>
   #     [{:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>2}},
   #      {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}]}        

Define selection_criterion as desired, and possible change max_by to min_by.

The steps are as follows.

 b = a[:results]
   # => [{:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>1}},
   #     {:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>2}},
   #     {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}] 
 c = b.group_by { |h| h[:employee][:employee_id] }
   #=> {"A-00016"=>[{:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>1}},
   #                {:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>2}}],
   #    "C-00017"=>[{:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}]}
 d = c.values
   #=> [[{:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>1}},
   #     {:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>2}}],
   #    [{:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}]] 
 e = d.map { |arr| arr.max_by { |h| selection_criterion(h[:employee]) } }
   #=> [{:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>2}},
   #    {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}]
 { results: e }
   #=> {:results=>
   #     [{:employee=>{:name=>"A", :employee_id=>"A-00016", :title=>2}},
   #      {:employee=>{:name=>"C", :employee_id=>"C-00017", :title=>3}}]}        

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.