[
{
"branchCode": "111",
"referenceNumber": "2222",
"comments": "Write Off",
"transactionDate": "2022-02-23T00:00:00",
"amount": 0,
"accountCode": "MMMM",
"accountName": "Metal MMMM",
"allocations": [
{
"branchCode": "111",
"referenceNumber": "3333",
"allocationDate": "2022-02-23T09:23:57",
"ledgerTypeDescription": "Client",
"accountCode": "MMMM",
"accountName": "Metal MMMM",
"amount": -0.02
}
]
},
{
"branchCode": "222",
"referenceNumber": "33333",
"comments": "To write off historic med fee $0",
"transactionDate": "2022-03-24T00:00:00",
"amount": 0,
"accountCode": "OOOO",
"accountName": "Oranga OOOO - AEP",
"allocations": [
{
"branchCode": "222",
"referenceNumber": "456",
"allocationDate": "2022-03-24T10:39:02",
"ledgerTypeDescription": "Client",
"accountCode": "OOOOP",
"accountName": "Oranga 0000 - AEP",
"amount": 94.88
}
]
}
]
在上面的JSON响应中,我需要找到在整个响应中完成了多少分配。我可以使用for循环来实现吗?如果可以,如何使用它来遍历数组,然后计算分配项
我尝试使用Object.keys(jsondata[0]).length)方法。但这对于查找特定块中的项的长度很有用。但我不明白如何遍历数组并找到分配的数量
3条答案
按热度按时间wlwcrazw1#
另一种选择是使用
flatMap()
来获取所有可以调用.length
的allocations
:cvxl0en22#
你可以像这样使用
reduce
。请注意,如果接收到的是字符串,则可能需要对数据使用JSON.parse
。ajsxfq5m3#
您也可以在
Array.forEach()
的帮助下实现此要求。现场演示**:**