0

I have two nested arrays which look like

a = [["Codereview", 72], ["Exercise", 380], ["Prework", 220], ["Retrospective", 36]]
b = [["Codereview", 72], ["Exercise", 335], ["Prework", 118], ["Retrospective", 36]]

You can assume that the length of the arrays is always the same.

I would like to generate an array of hashes from these two nested arrays which should look like

data = [
 { name: "Codereview", total_marks: 72, student_marks: 72 },
 { name: "Exercise", total_marks: 380, student_marks:  335 },
 { name: "Prework", total_marks: 220, student_marks: 118 },
 { name: "Retrospective", total_marks: 36, student_marks: 36 }
]

I don't have an idea how to go about this. Any help would be appreciated.

3 Answers 3

1

This assumes that the length of both arrays is the same and their order matches up (as far as names go).

> a.zip(b).map{|e| {name: e.first.first, total_marks: e.first.last, student_marks: e.last.last}}

=> [{:name=>"Codereview", :total_marks=>72, :student_marks=>72},
 {:name=>"Exercise", :total_marks=>380, :student_marks=>335},
 {:name=>"Prework", :total_marks=>220, :student_marks=>118},
 {:name=>"Retrospective", :total_marks=>36, :student_marks=>36}]
Sign up to request clarification or add additional context in comments.

1 Comment

If they don't necessarily have the same order, a.zip(b.sort_by { |bname,_| a.index { |aname,_| aname==bname } }).map.....
0

simple way.copying from console

 pry(main)> data =[]
 => []
 3] pry(main)> b_hash =b.to_h
 => {"Codereview"=>72, "Exercise"=>335, "Prework"=>118,Retrospective"=>36}
 pry(main)> a.each do |array|
 [8] pry(main)*   h = Hash.new()
 [8] pry(main)*   h["name"] = array.first
 [8] pry(main)*   h["total_marks"] = array.last
 [8] pry(main)*   h["student_marks"] = b_hash[array.first]
 [8] pry(main)*   data << h
 [8] pry(main)* end
 9] pry(main)> data
 => [{"name"=>"Codereview", "total_marks"=>72, "student_marks"=>72},
 {"name"=>"Exercise", "total_marks"=>380, "student_marks"=>335},
 {"name"=>"Prework", "total_marks"=>220, "student_marks"=>118},
 {"name"=>"Retrospective", "total_marks"=>36, "student_marks"=>36}]

Comments

0
a = [["Codereview", 72], ["Exercise", 380], ["Prework", 220], ["Retrospective", 36]]
b = [["Prework", 118], ["Retrospective", 36], ["Codereview", 72], ["Exercise", 335]]

g = a.each_with_object({}) { |(str, val), h| h[str] = { "name"=>str, "total_marks"=>val } }
  #=> {"Codereview"=>{"name"=>"Codereview", "total_marks"=>72},
  #    "Exercise"=>{"name"=>"Exercise", "total_marks"=>380},
  #    "Prework"=>{"name"=>"Prework", "total_marks"=>220},
  #    "Retrospective"=>{"name"=>"Retrospective", "total_marks"=>36}} 
b.each_with_object(g) { |(str, val),h| h[str]["student_marks"] = val }.values
  #=> [{"name"=>"Codereview", "total_marks"=>72, "student_marks"=>72},
  #    {"name"=>"Exercise", "total_marks"=>380, "student_marks"=>335},
  #    {"name"=>"Prework", "total_marks"=>220, "student_marks"=>118},
  #    {"name"=>"Retrospective", "total_marks"=>36, "student_marks"=>36}]

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.