我一直在使用map并减少了一些目前运行良好的对象和数组,但是我遇到了一个数组的问题。
此处的数据示例:
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作为一个对象。)
有人可以提出一个方法吗?
谢谢!
答案 0 :(得分:2)
你可以通过首先展平数组然后减少:
来做得非常整齐[].concat(...arr)
.map(invoice => invoice.invoicedNet.amountString)
.reduce((a, b) => a + b)
答案 1 :(得分: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);

答案 2 :(得分: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))