2017-08-04 87 views
1

我一直在使用映射和減少一直到目前爲止工作得很好的對象和數組,但是我遇到了一個數組的問題。數據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作爲一個對象)。

任何人都可以提出一種方法嗎?

謝謝!

回答

1

你需要循環兩個數組。

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 }]], 
 
    sum = arr.reduce(function (a, b) { 
 
     b.forEach(function (c) { 
 
      a += c.invoicedNet.amountString; 
 
     }); 
 
     return a; 
 
    }, 0); 
 

 
console.log(sum);

+0

謝謝!!!這工作完美! –

2

可以通過第一平整的陣列,然後降低做到這一點很整齊:

[].concat(...arr) 
    .map(invoice => invoice.invoicedNet.amountString) 
    .reduce((a, b) => a + b) 
+0

我不是一個過早的優化,但你的'map'ping真的沒有必要 - 爲什麼兩個循環,當一個足夠? – Adam

+0

只需找到它整潔。更清楚的是,您正在提取該值並進行求和。 – csander

1

壓扁你的陣列,進而降低:

[].concat(...arr).reduce((a, { invoicedNet: { amountString }}) => a + amountString, 0) 

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

 
console.log([].concat(...arr).reduce((a, { invoicedNet: { amountString }}) => a + amountString, 0))