2015-07-01 93 views
1

予先從以下:使用查找陣列去通的API與散列數組合並哈希基於值在共享密鑰

@todd.each do |x| 
    @num << x[:num] 
end 

然後:

@prod = [] 
@num = [] 
@todd = [{:sellersku=>"2273500028", :num=>"B0076E32F8", :price=>"15.49"}, 
{:sellersku=>"5154464774", :num=>"B00013J6HY", :price=>"445.94"}] 

然後創建一個查找數組將各種數據加載到結果中:

@num.each do |x| 
call_api(x) #I left out the code that populates the variables below.. 
results = {:num => x, 
:vendor => vendor, :type => type, :color => @color, :image => hi_image 
} 
end 

當以上完成調用api時,我想合併@ todd數組和結果數組放入散列的@prod數組中。造成這種情況。

[{:sellersku=>"2273500028", :num=>"B0076E32F8", :price=>"15.49", 
:vendor => "Boss", :type => "Shoes", :color => "Blue", :image => boss.jpg}, 
{:sellersku=>"5154464774", :num=>"B00013J6HY", :price=>"34.49", 
:vendor => "Converse", :type => "Shirt", :color => "Orange", 
:image => cons.jpg}] 
+2

我不知道你想做什麼?爲什麼不能簡單地填充現有@todd哈希(或複製)與信息:'@prod = @ todd.map {| H | h.merge(call_api h [:num])}'...假設'call_api'返回一個安靜的值。你試圖達到什麼樣的差異? – Myst

+0

所以我遇到的問題是發送數量通過調用API,然後當結果哈希得到填充合併@託德哈希,以便在每個和正確的價格上的數字匹配與正確的數字合併。有道理? – ToddT

回答

0
@prod = [] 
@num = [] 
@todd = [ 
    {:sellersku=>"2273500028", :num=>"B0076E32F8", :price=>"15.49"}, 
    {:sellersku=>"5154464774", :num=>"B00013J6HY", :price=>"445.94"} 
] 

@todd.each do |hash| 
    serial_number = hash[:num] 

    # EXTERNAL CALL TO API 
    info = api_call(serial_number) #likely returns json 
    # parse into new hash called results 

    results = { 
    :vendor => vendor, 
    :type => type, 
    :color => @color, 
    :image => hi_image 
    } 

    @prod << hash.merge(results) 
end 

@prod 
# after iteration, @prod will have all the information you are looking for. 
相關問題