我一直在使用映射和減少一直到目前爲止工作得很好的對象和數組,但是我遇到了一個數組的問題。數據ES6減少數組中的嵌套對象
這裏舉例:
var arr =
[
[
{
"id": 6501511,
"invoiceId": {
"id": 1043773
},
"chargeBandType": "TIME",
"jobTaskId": {
"id": 19399852
},
"invoicedNet": {
"amountString": 0,
"currencyType": "USD"
},
"invoicedTaxOneOtherCurrency": null,
"invoicedTaxOne": {
"amountString": 0,
"currencyType": "USD"
},
"taxOneRate": 0.1
},
{
"id": 6501517,
"invoiceId": {
"id": 1043773
},
"chargeBandType": "TIME",
"jobTaskId": null,
"jobExpenseId": null,
"jobThirdPartyCostId": {
"id": 20602
},
"invoicedNet": {
"amountString": 0,
"currencyType": "USD"
},
"invoicedTaxOneOtherCurrency": null,
"invoicedTaxOne": {
"amountString": 0,
"currencyType": "USD"
},
"taxOneRate": 0.1
},
{
"id": 6501508,
"invoiceId": {
"id": 13773
},
"chargeBandType": "TIME",
"jobTaskId": {
"id": 19398574
},
"invoicedNet": {
"amountString": 30,
"currencyType": "USD"
},
"invoicedTaxOneOtherCurrency": null,
"invoicedTaxOne": {
"amountString": 3,
"currencyType": "USD"
},
"taxOneRate": 0.1
},
{
"id": 65014,
"invoiceId": {
"id": 104
},
"chargeBandType": "TIME",
"jobTaskId": null,
"jobExpenseId": null,
"jobThirdPartyCostId": {
"id": 206
},
"invoicedNet": {
"amountString": 0,
"currencyType": "USD"
},
"invoicedTaxOneOtherCurrency": null,
"invoicedTaxOne": {
"amountString": 0,
"currencyType": "USD"
},
"taxOneRate": 0.1
}],
[
{
"id": 6483,
"invoiceId": {
"id": 1042400
},
"chargeBandType": "TIME",
"jobTaskId": {
"id": 198574
},
"invoicedNet": {
"amountString": 100,
"currencyType": "USD"
},
"invoicedTaxOneOtherCurrency": null,
"invoicedTaxOne": {
"amountString": 10,
"currencyType": "USD"
},
"taxOneRate": 0.1
}
]
];
我試圖減少invoicedNet.amountString的值,這將帶來130之和在上述情況下。
我已經嘗試了很多方法來解決這個,包括類似如下的功能:
var sum = arr.reduce(function(a, b) {
return a += b.invoicedNet.amountString;
}, 0);
但是,不管我如何努力這一點,我不斷收到錯誤:
TypeError: Cannot read property 'amountString' of undefined
(雖然它似乎拿起b.invoicedNet作爲一個對象)。
任何人都可以提出一種方法嗎?
謝謝!
謝謝!!!這工作完美! –