2017-08-04 8 views
1

マップと縮小を使用していますが、これまでのところうまくいきましたが、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 
} 
] 
]; 

私は、上記の場合には130の合計をもたらすだろうinvoicedNet.amountStringの値を、軽減しようとしています。

TypeError: Cannot read property 'amountString' of undefined 

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

しかし、関係なく、私はこれを試してみてくださいどのように、私はエラーを取得しておくん:

は、私は以下と同様の機能を含めて、この仕事には多くの方法を、試してみました(それは、しかし、オブジェクトとして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))

関連する問題