如下
<script>
const listData=[
{name:"name1",type:"收",currency:"cny",amount:152}, //1
{name:"name1",type:"收",currency:"cny",amount:12}, //2
{name:"name1",type:"收",currency:"cny",amount:125}, //3
{name:"name1",type:"付",currency:"jpy",amount:1112}, //4
{name:"name1",type:"付",currency:"eur",amount:1112}, //5
{name:"name1",type:"付",currency:"usd",amount:12}, //6
{name:"name4",type:"付",currency:"eur",amount:142}, //7
{name:"name2",type:"付",currency:"eur",amount:132}, //8
];
// 得到的预期结果希望是
// 收 name1 cny 289 //1 ,2 ,3 合并
// 付 name1 jpy 1112 //4
// 付 name1 eur 1112 //5
// 付 name1 usd 12 //6
// 付 name4 eur 142 //7
// 付 name2 eur 132 //8
//条件就是 把 name, type, currency相同的都合并起来
</script>
我暂时想到了group by ->group by ->group by 三次搂出来,有没有大哥有好的想法
已处理 thx
//币种
let _currency= [...new Set(preApprovalArr.value.map((it: { CurrencyDesc: any }) => it.CurrencyDesc) as [])];
// 类型
let _billFeeTypeDesc= [...new Set(preApprovalArr.value.map((it: { BillFeeTypeDesc: any }) => it.BillFeeTypeDesc) as [])];
let __billFee=getGroup(preApprovalArr.value,'BillFeeTypeDesc');
_billFeeTypeDesc.forEach((element:any) => {
let __currency= getGroup(__billFee[element],'CurrencyDesc');
_currency.forEach((currencyItem:any) => {
console.log(element,currencyItem,__currency[currencyItem],"currencyItem");
});
});
}
const getGroup=(data:any,key:any)=>{
let groups={};
data.forEach((c:any)=>{
let value=c[key];
groups[value]=groups[value]||[];
groups[value].push(c);
});
return groups;
}
@求你别想我:
@求你别想我:
var resulweeeet1 = Enumerable.from(val).groupBy(`{OrderId:$.OrderId,OrderType:$.OrderType,InputCompanyId: $.InputCompanyId,SettlementCompanyId: $.SettlementCompanyId,FeeType: $.FeeType,IsCurrency: $.IsCurrency,IsTax: $.IsTax,IsPositive: $.IsPositive,TaxRate: $.TaxRate}` as any,
null as any,
function (key:any, g) {
var result = {
orderId: key.OrderId,
orderType: key.OrderType,
inputCompanyId: key.InputCompanyId,
settlementCompanyId: key.SettlementCompanyId,
isCurrency: key.IsCurrency,
isTax: key.IsTax,
isPositive: key.IsPositive,
taxRate: key.TaxRate,
amount: g.sum("$.Amount" as any),
quantity: g.sum("$.Quantity" as any),
unitPrice: g.sum("$.UnitPrice" as any)
}
return result;
},function (x:any) {
// group by 几个 返回几个
return x.OrderId + ':' + x.OrderType + ':' + x.InputCompanyId + ':' + x.SettlementCompanyId + ':'
+ x.IsCurrency + ':' + x.IsTax + ':' + x.IsPositive+ ':' + x.TaxRate
}).toArray();
我们想法好像是一样的额
thx!
@敲代码挣彩礼: 但是我没有见过世面,我猜是这样的
@echo_lovely:
我也不知道,反正就这样吧 / 滑稽
const list = [
{ name: "name1", type: "收", currency: "cny", amount: 152 }, //1
{ name: "name1", type: "收", currency: "cny", amount: 12 }, //2
{ name: "name1", type: "收", currency: "cny", amount: 125 }, //3
{ name: "name1", type: "付", currency: "jpy", amount: 1112 }, //4
{ name: "name1", type: "付", currency: "eur", amount: 1112 }, //5
{ name: "name1", type: "付", currency: "usd", amount: 12 }, //6
{ name: "name4", type: "付", currency: "eur", amount: 142 }, //7
{ name: "name2", type: "付", currency: "eur", amount: 132 }, //8
]
const map = new Map()
for (const item of list) {
const { name, type, currency, amount } = item
const key = name + type + currency
if(!map.has(key)) map.set(key, item)
else map.get(key).amount += amount
}
console.log(map.values());