簡體   English   中英

ES6 減少數組中的嵌套對象

[英]ES6 Reduce nested objects in an array

我一直在使用 map 並減少了一些對象和 arrays 到目前為止運行良好,但是我遇到了一個陣列的問題。

這里的數據示例:

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 選為 object)。

任何人都可以建議一種方法嗎?

謝謝

您需要循環 arrays。

 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);

您可以通過首先展平 arrays 然后減少:

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

展平您的陣列,然后減少:

[].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))

var sum = arr.reduce(function(a, b) {
return a += b.invoicedNet.amountString;
}, 0);

上面的代碼不起作用,因為arr是一個多維數組。 它的元素也是一個數組。 對於這種類型的數組,您需要一個嵌套的 reducer。 用箭頭 function 可以寫成 -

const total = arr.reduce((sum, tasks) => tasks.reduce((previousSum, task) =>
              previousSum + task.invoicedNet.amountString, sum) , 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM