我怎么能根据另一个数组过滤和排序哈希数组?

时间:2016-02-15 15:07:12

标签: arrays ruby hash

我想过滤和排序哈希数组:

[
  {"_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
    }

2 个答案:

答案 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