gpt4 book ai didi

javascript - 按国家数量对各大洲进行排序

转载 作者:行者123 更新时间:2023-11-28 07:17:28 31 4
gpt4 key购买 nike

我有一个国家/地区列表:

"country": [
{
"countryCode": "AD",
"countryName": "Andorra",
"currencyCode": "EUR",
"population": "84000",
"capital": "Andorra la Vella",
"continentName": "Europe",
"continent": "EU",
"areaInSqKm": "468.0",
"languages": [
"ca"
]
},
{
"countryCode": "AE",
"countryName": "United Arab Emirates",
"currencyCode": "AED",
"population": "4975593",
"capital": "Abu Dhabi",
"continentName": "Asia",
"continent": "AS",
"areaInSqKm": "82880.0",
"languages": [
"ar-AE",
"fa",
"en",
"hi",
"ur"
]
},
etc.

现在我必须用 JSON 编写一个 Map/Reduce 函数才能获得如下输出:

键:Afrika 值:xxx键:亚洲 值:xxx等等

其中“值”代表每个大陆的国家/地区数量。

我已经尝试过这个 map 功能:

function(doc) {
var a
{
for (a in doc.country){
emit(doc.country[a].continentName, 1)
};
}
}

最佳答案

map 功能:

function(doc) {
var country = doc.country;
var i;
for (i in country) {
emit (country[i].continentName, 1)
};
}

然后你必须使用Reduce Function来减少它:

function(keys, values) {
return sum(values);}

关于javascript - 按国家数量对各大洲进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30695620/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com