我有一个杂烩:
{
"grey" => ["anf_94748_01_prod1", "anf_94748_01_model1", "anf_94748_01_model2"],
"heather blue" => ["anf_106537_01_prod1", "anf_106537_01_model1", "anf_106537_01_model2"],
"indigo" => [],
"dark grey" => ["anf_94747_01_prod1"]
}
如何根据图像ID数组替换其值:
[317, 318, 319, 320, 340, 358, 365]
如果哈希数组为空,则跳过它并转到下一个哈希键,并为该图像分配id期望输出为:
{
"grey" => [317, 318, 319],
"heather blue" => [320, 340, 358],
"indigo" => [],
"dark grey" => [365]
}
最佳答案
h = {
"grey" => ["anf_94748_01_prod1", "anf_94748_01_model1", "anf_94748_01_model2"],
"heather blue" => ["anf_106537_01_prod1", "anf_106537_01_model1", "anf_106537_01_model2"],
"indigo" => [],
"dark grey" => ["anf_94747_01_prod1"]
}
a = [317, 318, 319, 320, 340, 358, 365]
h.each_value{|v| v.map!{a.shift}}
# =>
# {
# "grey"=>[317, 318, 319],
# "heather blue"=>[320, 340, 358],
# "indigo"=>[],
# "dark grey"=>[365]
# }