javascript - 计算先前元素的数量
问题描述
我有一个这样的对象数组:
const data = [
{
"continent": "Europe",
"year": 2016,
"state": " mx l",
"value": 93.10611646419025
},
{
"continent": "Europe",
"year": 2016,
"state": "Q xe",
"value": 46.14966763264863
},
{
"continent": "Europe",
"year": 2017,
"state": " mx l",
"value": 29.392192664199012
},
{
"continent": "Europe",
"year": 2017,
"state": "Q xe",
"value": 14.676226327897535
},
{
"continent": "Europe",
"year": 2018,
"state": " mx l",
"value": 7.9210338610822495
},
{
"continent": "Europe",
"year": 2018,
"state": "Q xe",
"value": 42.77961684001821
},
{
"continent": "Europe",
"year": 2019,
"state": " mx l",
"value": 30.195477072260847
},
{
"continent": "Europe",
"year": 2019,
"state": "Q xe",
"value": 0.4764215061746846
},
{
"continent": "Asia",
"year": 2016,
"state": "Mxhvisio",
"value": 52.184301395612096
},
{
"continent": "Asia",
"year": 2016,
"state": "Velgbvsy",
"value": 66.56540671530074
},
{
"continent": "Asia",
"year": 2016,
"state": "Otxenpwe",
"value": 81.28417729926467
},
{
"continent": "Asia",
"year": 2017,
"state": "Mxhvisio",
"value": 24.959281347996697
},
{
"continent": "Asia",
"year": 2017,
"state": "Velgbvsy",
"value": 62.620083230638166
},
{
"continent": "Asia",
"year": 2017,
"state": "Otxenpwe",
"value": 29.259822764307053
},
{
"continent": "Asia",
"year": 2018,
"state": "Mxhvisio",
"value": 97.99032287910472
},
{
"continent": "Asia",
"year": 2018,
"state": "Velgbvsy",
"value": 15.553958337919838
},
{
"continent": "Asia",
"year": 2018,
"state": "Otxenpwe",
"value": 1.0460838512473591
},
{
"continent": "Asia",
"year": 2019,
"state": "Mxhvisio",
"value": 36.11846533794167
},
{
"continent": "Asia",
"year": 2019,
"state": "Velgbvsy",
"value": 25.467981394020022
},
{
"continent": "Asia",
"year": 2019,
"state": "Otxenpwe",
"value": 59.55173397523441
},
{
"continent": "Africa",
"year": 2016,
"state": "Oqkaqap",
"value": 66.8220176856509
},
{
"continent": "Africa",
"year": 2016,
"state": " vnzkxo",
"value": 11.062951843116519
},
{
"continent": "Africa",
"year": 2016,
"state": "Juucqrd",
"value": 8.482606846746087
},
{
"continent": "Africa",
"year": 2017,
"state": "Oqkaqap",
"value": 78.48483030953402
},
{
"continent": "Africa",
"year": 2017,
"state": " vnzkxo",
"value": 93.20229532997375
},
{
"continent": "Africa",
"year": 2017,
"state": "Juucqrd",
"value": 96.36196870652273
},
{
"continent": "Africa",
"year": 2018,
"state": "Oqkaqap",
"value": 18.806971985682488
},
{
"continent": "Africa",
"year": 2018,
"state": " vnzkxo",
"value": 59.864704301091365
},
{
"continent": "Africa",
"year": 2018,
"state": "Juucqrd",
"value": 77.49958555283216
},
{
"continent": "Africa",
"year": 2019,
"state": "Oqkaqap",
"value": 55.113253844664015
},
{
"continent": "Africa",
"year": 2019,
"state": " vnzkxo",
"value": 20.65153716524726
},
{
"continent": "Africa",
"year": 2019,
"state": "Juucqrd",
"value": 1.6831843892751275
},
{
"continent": "Americas",
"year": 2016,
"state": "Ktaq np",
"value": 27.574234534710442
},
{
"continent": "Americas",
"year": 2016,
"state": "Xjzxccd",
"value": 56.92744198752449
},
{
"continent": "Americas",
"year": 2017,
"state": "Ktaq np",
"value": 41.10078504806991
},
{
"continent": "Americas",
"year": 2017,
"state": "Xjzxccd",
"value": 28.56665484963914
},
{
"continent": "Americas",
"year": 2018,
"state": "Ktaq np",
"value": 79.81517223034149
},
{
"continent": "Americas",
"year": 2018,
"state": "Xjzxccd",
"value": 17.274959818275715
},
{
"continent": "Americas",
"year": 2019,
"state": "Ktaq np",
"value": 48.15827138437179
},
{
"continent": "Americas",
"year": 2019,
"state": "Xjzxccd",
"value": 57.19057047246159
}
]
和一系列大陆:
const continents = ['Europe', 'Asia', 'Africa', 'Americas']
我以这种方式迭代continents
数组:
continents.map((continent, i) => {
const numOfPreviousStates = ??
})
numOfPreviousStates
应该包含在当前大陆之前的州的数量。
所以,如果continent = 'Europe'
那么numOfPreviousStates = 0
,如果continent = 'Asia'
那么numOfPreviousStates = 0+8=8
,如果continent = 'Africa'
那么numOfPreviousStates = 8+12=20
,如果continent = 'Americas'
那么numOfPreviousStates = 8+12+12 = 32
。
我怎样才能做到这一点?我想我可以使用reduce
,但是如何使用?
解决方案
您可以像这样使用过滤:
continents.map((continent, i) => {
return data.filter(e => continents.indexOf(e.continent)<i).length;
})
推荐阅读
- python-3.x - 如何部分清理命令提示符
- ios - Xcode 创建通用 Xcode 存档而不是 iOS App Archive?
- spring - Spring Security 不要求登录名和密码
- swift - 打印嵌套枚举中的所有案例 - Swift
- javascript - 在 React 中使用 Hooks 实现倒数计时器
- java - 在每个请求之前进行 Spring MVC Session 检查
- python - 无法更改列“id”:在 Django 上的表错误的外键约束中使用
- extendscript - 如何正确处理文件名编码?
- ms-office - 无法以只读模式共享带有自动打开标签的文档
- java - Libgdx:为什么 Actions.parallel 中的动作会不断重复自己?