gpt4 book ai didi

javascript - 计算先前元素的数量

转载 作者:数据小太阳 更新时间:2023-10-29 06:14:07 25 4
gpt4 key购买 nike

我有一个这样的对象数组:

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;
})

关于javascript - 计算先前元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54403635/

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