首页 新闻 会员 周边 捐助

前端数组如果筛选过滤成这样的数据

0
[已解决问题] 解决于 2022-08-19 10:54

如下

    <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 三次搂出来,有没有大哥有好的想法

永远跟党走i的主页 永远跟党走i | 小虾三级 | 园豆:1542
提问于:2022-08-18 22:57
< >
分享
最佳答案
0

已处理 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;
}


永远跟党走i | 小虾三级 |园豆:1542 | 2022-08-19 10:54

永远跟党走i | 园豆:1542 (小虾三级) | 2022-08-19 11:05

@求你别想我:

超级nb的 https://www.npmjs.com/package/linq

永远跟党走i | 园豆:1542 (小虾三级) | 2022-10-15 14:47
永远跟党走i | 园豆:1542 (小虾三级) | 2022-10-15 14:50

@求你别想我:

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

永远跟党走i | 园豆:1542 (小虾三级) | 2022-10-15 16:59
其他回答(2)
0

我们想法好像是一样的额

echo_lovely | 园豆:1534 (小虾三级) | 2022-08-19 08:27

thx!

支持(0) 反对(0) 永远跟党走i | 园豆:1542 (小虾三级) | 2022-08-19 10:54

@敲代码挣彩礼: 但是我没有见过世面,我猜是这样的

支持(0) 反对(0) echo_lovely | 园豆:1534 (小虾三级) | 2022-08-19 10:55

@echo_lovely:
我也不知道,反正就这样吧 / 滑稽

支持(0) 反对(0) 永远跟党走i | 园豆:1542 (小虾三级) | 2022-08-19 11:06
0
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());

zhangxinloop | 园豆:174 (初学一级) | 2023-08-11 17:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册