我正在用JavaScript开发一个项目,我想总结每种付款方式针对该付款类型的金额,例如现金,卡或客户帐户。
我希望表格看起来像这样
付款方式--------金额
现金--------------------- 30
卡---------------------- 200
帐户------------ ----- 2000
运行此代码时
User::Pine
我刚得到第一个结果:
付款方式--------金额
现金--------------------- 30
JSON数据
// Get sales for curent period
getSalesBetween(startTime_b: string, endTime_a: string) {
if (this._sales) {
return Promise.resolve(this._sales);
}
let startTime = "PAY_" + startTime_b;
if (!endTime_a) {
this.endTime_b = startTime;
} else {
this.endTime_b = "PAY_" + endTime_a;
}
return new Promise(resolve => {
this.db.query('my_sales_payments_by_date', {
startkey: startTime,
endkey: this.endTime_b + '\ufff0',
include_docs: true
}).then((result) => {
console.log('Get sales:: ', JSON.stringify(result));
this._sales = [];
this.payment = [];
let tcash = 0,
tcard = 0,
tacc = 0,
total = 0;
let paymentType = '';
let i = 0;
result.rows.map(row => {
total = total + row.doc.amount;
paymentType = row.doc.paymentOption;
console.log("Total:: ", total);
if (row.doc.paymentOption = 'cash') {
paymentType = 'Cash';
tcash = tcash + +row.doc.amount;
console.log('cash - ' + row.doc.amount)
} else if (row.doc.paymentOption = 'card') {
paymentType = 'Card';
tcard = tcard + +row.doc.amount;
console.log('Card - ' + row.doc.amount)
} else if (row.doc.paymentOption = 'account') {
paymentType = 'Account'
tacc = tacc + +row.doc.amount;
console.log('Account - ' + row.doc.amount)
}
});
this.payment.push({
paymentType: paymentType,
tcash: tcash,
tcard: tcard,
tacc: tacc,
tcashp: this.percentage(tcash, total),
tcardp: ((tcard / total) * 100).toFixed(2),
taccp: ((tacc / total) * 100).toFixed(2)
})
resolve(this.payment);
this.db.changes({
live: true,
since: 'now',
include_docs: true
}).on('change', this.onDatabaseChange);
}).catch((error) => {
console.log(error);
resolve(false);
});
}).catch((error) => {
console.log(error);
});
}
我该如何进行这项工作?我将不胜感激的任何帮助
答案 0 :(得分:0)
您可能需要像这样的东西?
let cash = 0;
let card = 0;
let account = 0;
for (const row of result.rows) {
switch (row.doc.paymentOption) {
case 'cash':
cash += row.doc.amount;
break;
case 'card':
card += row.doc.amount;
break;
case 'account':
account += row.doc.amount;
break;
default:
// Error
}
}
console.log(`Cash: ${cash}`);
console.log(`Card: ${card}`);
console.log(`Account: ${account}`);
这是假设row.doc.amount
字段始终包含有效的数值。
答案 1 :(得分:0)
您可以使用此- 遍历row属性并获取付款的类型和值。如果付款类型已经存在,则将其添加到对象中,否则它将创建该属性。
var obj={
"total_rows":59,
"offset":0,
"rows":[
{
"key":"PAY_2019-01-11T12:18:52.085Z",
"id":"PAY_2019-01-11T12:18:52.085Z",
"value":110,
"doc":{
"paymentStatus":true,
"paymentOption":"card",
"amount":110,
"tenderedTotal":110,
"time":"2019-01-11T12:18:52.085Z",
"orderId":"ORD_2019-01-11T12:18:52.085Z",
"type":"payment",
"_id":"PAY_2019-01-11T12:18:52.085Z",
"_rev":"1-9fdc73a415914311a80db1727fbc593b"
}
},
{
"key":"PAY_2019-01-11T16:27:29.553Z",
"id":"PAY_2019-01-11T16:27:29.553Z",
"value":66,
"doc":{
"paymentStatus":true,
"paymentOption":"cash",
"amount":66,
"tenderedTotal":66,
"time":"2019-01-11T16:27:29.553Z",
"orderId":"ORD_2019-01-11T16:27:29.553Z",
"type":"payment",
"_id":"PAY_2019-01-11T16:27:29.553Z",
"_rev":"1-a58e3811d11c4ca3a92bd1e206e6e0f3"
}
},
{
"key":"PAY_2019-01-11T18:12:13.716Z",
"id":"PAY_2019-01-11T18:12:13.716Z",
"value":60,
"doc":{
"paymentStatus":true,
"paymentOption":"account",
"amount":60,
"tenderedTotal":152,
"time":"2019-01-11T18:12:13.716Z",
"orderId":"ORD_2019-01-11T18:12:13.716Z",
"type":"payment",
"fromCustomerAccount":true,
"customerPayment":false,
"grandTotal":60,
"customer":{
"type":"account",
"account_type":"customer",
"fname":"Mukopaje",
"lname":"Singogo",
"phone":"974776247",
"email":"mukopaje@gmail.com",
"region":"Other",
"city":"Lusaka",
"balance":0,
"status":true,
"index":0,
"_id":"CST_2019-01-03T11:27:22.924Z",
"_rev":"4-9453c05f151242a39bc5511323bbb820"
},
"customerPaid":152,
"customer_id":"CST_2019-01-03T11:27:22.924Z",
"credeb":"debit",
"_id":"PAY_2019-01-11T18:12:13.716Z",
"_rev":"1-0486777aaf1444bdaa8bcd35dd18c1d9"
}
}
]
}
var a=obj.rows;
var k={};
a.forEach((e)=>{
if(!k.hasOwnProperty(e.doc.paymentOption))
{
k[e.doc.paymentOption]=0;
}
if(k.hasOwnProperty(e.doc.paymentOption))
{
k[e.doc.paymentOption]=Number(k[e.doc.paymentOption])+Number(e.value);
}
})
console.log(k)