我得到了一個哈希(見下文),我需要組,但我不知道如何去做。 我將在iOS中使用此數組作爲表格部分(Rubymotion)。如何對散列進行分組?
散列看起來是這樣的,我想按職業:
[{"firstname" => "John", "lastname" => "Wayne", "profession" => "actor"},{"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"},{"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}]
我需要它看起來像這樣:
["actor" => [{"firstname" => "John", "lastname" => "Wayne", "profession" => "actor"},{"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"}], "director" =>[{"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}]
更新
這將是非常好,如果我能得到這個結構:
{
"profession" => "actor",
"people" =>
[
{"firstname"=>"John", "lastname"=>"Wayne", "profession"=>"actor"},
{"firstname" => "Oliva", "lastname" => "Newton", "profession" => "actor"}
],
"profession" => "director",
"people" =>
[
{"firstname" => "Terry", "lastname" => "Gilliam", "profession" => "director"}
]
}
您輸入有錯構 –
你更新的結構更是雪上加霜。 – sawa
你不能有這樣的結構,''people''鍵不能在同一個散列表中出現一次以上 – mdesantis