- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个这样的对象数组:
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/
我的应用将 SceneKit 内容的“页面”与图像和文本交替。当我从图像页面前进到新的 SceneKit 页面时,前一个 SceneKit 页面中的内容会短暂显示,然后被新内容替换。时髦。 我只使用一
我正在尝试处理(在 C# 中)包含一些数字数据的大型数据文件。给定一个整数数组,如何对其进行拆分/分组,以便如果下一个 n(两个或更多)是负数,则前一个 n 元素被分组。例如,在下面的数组中,应该使用
刚接触promises,研究过。所以我的代码和我的理解: sql.connect(config).then(function(connection) { return connection.req
目前我在 if (roobaf) block 中有一些代码,这取决于 foo 和 bar 是否为假。我可以在 block 内再次检查这些条件,但感觉像是不必要的代码重复。 if (foo) {
我是一名优秀的程序员,十分优秀!