我將如何減少一堆深度嵌套數組並獲得某些鍵的最大值? 這裏是我的數據格式:javascript d3從多嵌套數組/對象數據集中獲取最大值
var data = [
{
"category": "Cat 1",
"subcategories": [
{
"subcategory": "Subcate 1 a",
"problems": [
{
"problem": "Problem 1 a 1",
"total": 3,
"breakdown": {
"moderate": 1,
"severe": 2
}
},
{
"problem": "Problem 1 a 2",
"total": 6,
"breakdown": {
"moderate": 5,
"severe": 1
}
}
]
}
]
},
{
"category": "Cat 2",
"subcategories": [
{
"subcategory": "Subcate 2 a",
"problems": [
{
"problem": "Problem 2 a 1",
"total": 8,
"breakdown": {
"moderate": 5,
"severe": 3
}
}
]
},
{
"subcategory": "Subcat 2 b",
"problems": [
{
"problem": "Problem 2 b 1",
"total": 4,
"breakdown": {
"moderate": 1,
"severe": 3
}
},
{
"problem": "Problem 2 b 2",
"total": 2,
"breakdown": {
"moderate": 2,
"severe": 0
}
}
]
}
]
}
]
如何我會得到「中等」和「嚴重」的每一個值的數組?所以在這個例子中,我需要
[1,2,5,1,5,3,1,3,2,0]
然後,我想做d3.max(數組),以獲得它的最大價值。
那肯定做的工作,感謝你們爲您輸入。 – rowyourboat
雖然這確實工作正常,但我最初發布的問題是因爲我想知道是否有避免在for循環中執行循環的方法。我一直在玩複雜的減少功能,並不能相當破解它。 – rowyourboat