2015-04-08 59 views
-4

我有一個數組,我想,以配合在多維數組的散列的其他陣列id鍵的值哈希鍵和值哈希的多維數組,合併基於Ruby的

input = [ 
    [ {"id"=>"1","name"=>"a"}, 
    {"id"=>"2","name"=>"b"}, 
    {"id"=>"3","name"=>"c"}, 
    {"id"=>"4","name"=>"d"}, 
    {"id"=>"5","name"=>"e"}, 
    {"id"=>"6","name"=>"f"} 
    ], 
    [ {"id"=>"3","hoby"=>"AA"}, 
    {"id"=>"3","hoby"=>"BB"}, 
    {"id"=>"1","hoby"=>"CC"}, 
    {"id"=>"1","hoby"=>"DD"}, 
    {"id"=>"4","hoby"=>"EE"} 
    ], 
    [ {"id"=>"1","language"=>"A"}, 
    {"id"=>"1","language"=>"B"}, 
    {"id"=>"2","language"=>"B"}, 
    {"id"=>"2","language"=>"C"}, 
    {"id"=>"6","language"=>"D"} 
    ] 
] 

我需要一個像陣列輸出,

output = [ 
    {"id"=>"1","name"=>"a","id"=>"1","hoby"=>"CC","id"=>"1","language"=>"A","id"=>"1","language"=>"B"}, 
    {"id"=>"2","name"=>"b","id"=>"2","language"=>"B"}, 
    {"id"=>"3","name"=>"c","id"=>"3","hoby"=>"AA","id"=>"3","hoby"=>"BB"}, 
    {"id"=>"4","name"=>"d","id"=>"4","hoby"=>"EE"}, 
    {"id"=>"5","name"=>"e"}, 
    {"id"=>"6","name"=>"f","id"=>"6","language"=>"D"} 
] 

我已經寫了代碼對於這一點,

len = input.length - 1 
output = [] 
input[0].each do |value,index| 
    for i in 1..len 
    input[i].each do |j| 
     if value["id"] == j["id"] 
     output << value.merge(j) 
     end 
    end 
    end 
end 

但我我得到了錯誤的輸出數組。在多維數組中可能有任何數量的子數組。

感謝,

+1

用於'ID = 1'存在多個嗜好。哪個「愛好」會優先? – shivam

+2

您需要添加更多關於您的需求的信息:人們只是猜測你想要什麼,因爲你所要求的不是有效的數據結構。如果我是你,我會後退一步,描述你正試圖解決的問題,而不是說「如何將這些數據轉化爲這些數據?」。 –

回答

1

也許這可以幫助你。

input = [ 
    [ 
    {"id"=>"1","name"=>"a"}, 
    {"id"=>"2","name"=>"b"}, 
    {"id"=>"3","name"=>"c"}, 
    {"id"=>"4","name"=>"d"}, 
    {"id"=>"5","name"=>"e"}, 
    {"id"=>"6","name"=>"f"} 
    ], 
    [ 
    {"id"=>"3","hoby"=>"AA"}, 
    {"id"=>"3","hoby"=>"BB"}, 
    {"id"=>"1","hoby"=>"CC"}, 
    {"id"=>"1","hoby"=>"DD"}, 
    {"id"=>"4","hoby"=>"EE"} 
    ], 
    [ 
    {"id"=>"1","language"=>"A"}, 
    {"id"=>"1","language"=>"B"}, 
    {"id"=>"2","language"=>"B"}, 
    {"id"=>"2","language"=>"C"}, 
    {"id"=>"6","language"=>"D"} 
    ] 
] 

這樣就可以使您的「排序」結果。

output = {} 
input.flatten.each do |h| 
    output[h["id"]] = {} unless output[h["id"]] 
    output[h["id"]].merge!(h) 
end 

output.values 
# => [ 
# => {"id"=>"1", "name"=>"a", "hoby"=>"DD", "language"=>"B"}, 
# => {"id"=>"2", "name"=>"b", "language"=>"C"}, 
# => {"id"=>"3", "name"=>"c", "hoby"=>"BB"}, 
# => {"id"=>"4", "name"=>"d", "hoby"=>"EE"}, 
# => {"id"=>"5", "name"=>"e"}, 
# => {"id"=>"6", "name"=>"f", "language"=>"D"} 
# => ] 

但更好的方法是在輸入使用散列。你可以像輸入密鑰一樣定義輸入,如「hash」和「id」,所以如果你生成了數據,你沒有問題來排序。

成才這樣

{ 
    "1" => {"name" => "a", "hoby" => "DD", "language" => "B"} 
} 
2

首先 - 它是不可能有一個哈希兩個元素使用相同的密鑰。將值分配給某個鍵時,將使新值的下一個相同鍵的分配覆蓋上一個鍵。

讓我們考慮例如:

hash = {} 
hash["id"] = 1 
hash["id"] = 3 
hash["id"] = 5 

你會期望hash["id"]輸出什麼? 1,3,5或者也許[1, 3, 5]?它會輸出5,因爲這是唯一鍵的最後一個分配。

話雖如此,這是不可能出現多次存儲在您的哈希值,但你可以嘗試處理它的東西,如:

input.flatten 
    .group_by { |h| h["id"] } 
    .map do |k, a| 
     a.each_with_object({}) { |in_h, out_h| out_h.merge!(in_h) } 
    end 

這將導致與像哈希:

[{"id"=>"1", "name"=>"a", "hoby"=>"DD", "language"=>"B"}, 
{"id"=>"2", "name"=>"b", "language"=>"C"}, 
{"id"=>"3", "name"=>"c", "hoby"=>"BB"}, 
{"id"=>"4", "name"=>"d", "hoby"=>"EE"}, 
{"id"=>"5", "name"=>"e"}, 
{"id"=>"6", "name"=>"f", "language"=>"D"}] 

那麼,它不是你所期望的哈希,但至少它可能會讓你朝某個方向發展。

希望有幫助!