我想过滤和排序哈希数组:
[
{"_id"=>"THAILAND", "cities"=>["BANGKOK", "CHIANGMAI"]},
{"_id"=>"MALAYSIA", "cities"=>["JAKARTA", "KUALA_LUMPUR"]},
{"_id"=>"JAPAN", "cities"=>["HOKKAIDO", "FUKUOKA", "TOKYO", "IBARAKI", "ISHIGAKI", "OSAKA", "KUMAMOTO", "KAGOSHIMA", "NAGOYA", "OKINAWA"]},
{"_id"=>"HONGKONG", "cities"=>["HONGKONG"]},
{"_id"=>"KOREA", "cities"=>["SEOUL", "BUSAN"]},
{"_id"=>"TAIWAN", "cities"=>["KAOHSIUNG", "TAIPEI"]},
{"_id"=>"AUSTRALIA", "cities"=>["MELBOURNE", "GOLD_COAST", "PERTH", "SYDNEY"]},
{"_id"=>"MACAU", "cities"=>["MACAU"]},
{"_id"=>"SINGAPORE", "cities"=>["SINGAPORE"]}
]
根据数组:
["JAPAN", "THAILAND", "KOREA", "TAIWAN", "AUSTRALIA", "MACAU", "SINGAPORE"]
预期结果是:
[
{"_id"=>"JAPAN", "cities"=>["HOKKAIDO", "FUKUOKA", "TOKYO", "IBARAKI", "ISHIGAKI", "OSAKA", "KUMAMOTO", "KAGOSHIMA", "NAGOYA", "OKINAWA"]},
{"_id"=>"THAILAND", "cities"=>["BANGKOK", "CHIANGMAI"]},
{"_id"=>"KOREA", "cities"=>["SEOUL", "BUSAN"]},
{"_id"=>"TAIWAN", "cities"=>["KAOHSIUNG", "TAIPEI"]},
{"_id"=>"AUSTRALIA", "cities"=>["MELBOURNE", "GOLD_COAST", "PERTH", "SYDNEY"]},
{"_id"=>"MACAU", "cities"=>["MACAU"]},
{"_id"=>"SINGAPORE", "cities"=>["SINGAPORE"]}
]
我怎么能以优雅的方式做到这一点?
这就是我计划实施它的方式。
candidates = [
{"_id"=>"THAILAND", "cities"=>["BANGKOK", "CHIANGMAI"]},
{"_id"=>"MALAYSIA", "cities"=>["JAKARTA", "KUALA_LUMPUR"]},
{"_id"=>"JAPAN", "cities"=>["HOKKAIDO", "FUKUOKA", "TOKYO", "IBARAKI", "ISHIGAKI", "OSAKA", "KUMAMOTO", "KAGOSHIMA", "NAGOYA", "OKINAWA"]},
{"_id"=>"HONGKONG", "cities"=>["HONGKONG"]},
{"_id"=>"KOREA", "cities"=>["SEOUL", "BUSAN"]},
{"_id"=>"TAIWAN", "cities"=>["KAOHSIUNG", "TAIPEI"]},
{"_id"=>"AUSTRALIA", "cities"=>["MELBOURNE", "GOLD_COAST", "PERTH", "SYDNEY"]},
{"_id"=>"MACAU", "cities"=>["MACAU"]},
{"_id"=>"SINGAPORE", "cities"=>["SINGAPORE"]}
]
a = ["JAPAN", "THAILAND", "KOREA", "TAIWAN", "AUSTRALIA", "MACAU", "SINGAPORE"]
a.inject([]) { h, country
tmp_candidate = {}
candidates.each do |candidate|
if country == candidate['_id']
tmp_candidate = candidate
break
end
end
h << tmp_candidate
h
}
答案 0 :(得分:6)
a = ["JAPAN", "THAILAND", "KOREA", "TAIWAN", "AUSTRALIA", "MACAU", "SINGAPORE"]
[{"_id"=>"THAILAND", "cities"=>["BANGKOK", "CHIANGMAI"]},
{"_id"=>"MALAYSIA", "cities"=>["JAKARTA", "KUALA_LUMPUR"]},
{"_id"=>"JAPAN", "cities"=>["HOKKAIDO", "FUKUOKA", "TOKYO", "IBARAKI", "ISHIGAKI", "OSAKA", "KUMAMOTO", "KAGOSHIMA", "NAGOYA", "OKINAWA"]},
{"_id"=>"HONGKONG", "cities"=>["HONGKONG"]},
{"_id"=>"KOREA", "cities"=>["SEOUL", "BUSAN"]},
{"_id"=>"TAIWAN", "cities"=>["KAOHSIUNG", "TAIPEI"]},
{"_id"=>"AUSTRALIA", "cities"=>["MELBOURNE", "GOLD_COAST", "PERTH", "SYDNEY"]},
{"_id"=>"MACAU", "cities"=>["MACAU"]},
{"_id"=>"SINGAPORE", "cities"=>["SINGAPORE"]}]
.select{|h| a.index(h["_id"])}
.sort_by{|h| a.index(h["_id"])}
答案 1 :(得分:1)
如果您的第一个数组是arr
而第二个数组是countries
,
arr.each_with_object({}) { |g,h| h.update(g["_id"]=>g) }.
values_at(*countries).
compact
#=> [{"_id"=>"JAPAN", "cities"=>[...]},
# {"_id"=>"THAILAND", "cities"=>[...]},
# {"_id"=>"KOREA", "cities"=>[...]},
# {"_id"=>"TAIWAN", "cities"=>[...]},
# {"_id"=>"AUSTRALIA", "cities"=>[...]},
# {"_id"=>"MACAU", "cities"=>[...]},
# {"_id"=>"SINGAPORE", "cities"=>[...]}]
仅当.compact
中的一个或多个元素在countries
中没有相应的哈希值时,才需要 arr
。