我在 Ruby 中有一个哈希数组,如下所示:
domains = [
{ "country" => "Germany"},
{"country" => "United Kingdom"},
{"country" => "Hungary"},
{"country" => "United States"},
{"country" => "France"},
{"country" => "Germany"},
{"country" => "Slovakia"},
{"country" => "Hungary"},
{"country" => "United States"},
{"country" => "Norway"},
{"country" => "Germany"},
{"country" => "United Kingdom"},
{"country" => "Hungary"},
{"country" => "United States"},
{"country" => "Norway"}
]
编辑::
因此,如果它以这种格式返回(来自 CouchDB):
domains= {"total_rows":55717,"offset":0,"rows": [
{"country":"Germany"},
{"country":"United Kingdom"},
{"country":"Hungary"},
{"country":"United States"},\ \
{"country":"France"},
{"country":"Germany"},
{"country":"Slovakia"},
{"country":"Hungary"},
{"country":"United States"},
{"country":"Norway"},
{"country":"Germany"},
{"country":"United Kingdom"},
{"country":"Hungary"},
{"country":"United States"},
{"country":"Norway"}]
}
我怎样才能应用相同的过程。即获取嵌入在数组中的项目?
使用 Ruby,我可以对数组进行交互并删除重复的值,如下所示:
counted = Hash.new(0)
domains.each { |h| counted[h["country"]] += 1 }
counted = Hash[counted.map {|k,v| [k,v.to_s] }]
哪个输出是这样的:
{"Germany"=>"3",
"United Kingdom"=>"2",
"Hungary"=>"3",
"United States"=>"3",
"France"=>"1",
"Slovakia"=>"1",
"Norway"=>"2"}
我的问题是使用 Javascript 可能使用下划线之类的库来实现相同目标的最佳方法是什么?
此致,
卡尔斯基