Javascript:跨多个对象属性的事务摘要(groupby,sumby)

时间:2017-08-26 13:11:25

标签: javascript lodash javascript-objects

我有不同货币和交易所的一系列外汇交易:

[  
{buyCurr: "USD", sellCurr: "CAD", exchange: "Canada", buyunits: 1000, sellunits: 1200},  
{buyCurr: "EUR", sellCurr: "CAD", exchange: "Europe", buyunits: 10000, sellunits: 15000},  
{buyCurr: "GBP", sellCurr: "USD", exchange: "NYSE", buyunits: 5000, sellunits: 8000},  
{buyCurr: "USD", sellCurr: "INR", exchange: "BSE", buyunits: 3000, sellunits: 180000},  
{buyCurr: "USD", sellCurr: "JPY", exchange: "Japan", buyunits: 32000, sellunits: 1000000},  
{buyCurr: "RUB", sellCurr: "INR", exchange: "Russia", buyunits: 1000, sellunits: 1100},  
{buyCurr: "USD", sellCurr: "CNY", exchange: "China", buyunits: 3000, sellunits: 18000},  
{buyCurr: "CNY", sellCurr: "INR", exchange: "China", buyunits: 10000, sellunits: 100000},  
{buyCurr: "USD", sellCurr: "CAD", exchange: "Canada", buyunits: 1000, sellunits: 1200},  
{buyCurr: "EUR", sellCurr: "CAD", exchange: "Europe", buyunits: 10000, sellunits: 15000},  
{buyCurr: "GBP", sellCurr: "USD", exchange: "NYSE", buyunits: 5000, sellunits: 8000},  
{buyCurr: "USD", sellCurr: "INR", exchange: "BSE", buyunits: 3000, sellunits: 180000},  
{buyCurr: "USD", sellCurr: "JPY", exchange: "Japan", buyunits: 32000, sellunits: 1000000},  
{buyCurr: "RUB", sellCurr: "INR", exchange: "Russia", buyunits: 1000, sellunits: 1100},  
{buyCurr: "USD", sellCurr: "CNY", exchange: "China", buyunits: 3000, sellunits: 18000},  
{buyCurr: "CNY", sellCurr: "INR", exchange: "China", buyunits: 10000, sellunits: 100000},  
]

我想总结一下我在各种交易所和货币中的立场。所以结果如下:

[{
    curency : USD,
    exchange: Canada,
    units: 2000
},
{
    curency : CAD,
    exchange: Canada,
    units: -2400
},
{
    curency : USD,
    exchange: NYSE,
    units: -12000
},
......and so on
]

交易所的单位数量(货币(i)的买单位总和) - (sellunits(i)的总和)。 SO美元只会抵消美元。

我提出的一种方法是使用forEach遍历每个实例,过滤属性并添加/减少buyCurr和sellCurr。

我也尝试了lodash,但它只允许我做groupBy属性。

在javascript中有更好/更有效的方法吗?

2 个答案:

答案 0 :(得分:0)

您也可以使用一个.reduce执行此操作。由于您只是添加和减去值,因此您可以随时总结:

const input=[{buyCurr:"USD",sellCurr:"CAD",exchange:"Canada",buyunits:1e3,sellunits:1200},{buyCurr:"EUR",sellCurr:"CAD",exchange:"Europe",buyunits:1e4,sellunits:15e3},{buyCurr:"GBP",sellCurr:"USD",exchange:"NYSE",buyunits:5e3,sellunits:8e3},{buyCurr:"USD",sellCurr:"INR",exchange:"BSE",buyunits:3e3,sellunits:18e4},{buyCurr:"USD",sellCurr:"JPY",exchange:"Japan",buyunits:32e3,sellunits:1e6},{buyCurr:"RUB",sellCurr:"INR",exchange:"Russia",buyunits:1e3,sellunits:1100},{buyCurr:"USD",sellCurr:"CNY",exchange:"China",buyunits:3e3,sellunits:18e3},{buyCurr:"CNY",sellCurr:"INR",exchange:"China",buyunits:1e4,sellunits:1e5},{buyCurr:"USD",sellCurr:"CAD",exchange:"Canada",buyunits:1e3,sellunits:1200},{buyCurr:"EUR",sellCurr:"CAD",exchange:"Europe",buyunits:1e4,sellunits:15e3},{buyCurr:"GBP",sellCurr:"USD",exchange:"NYSE",buyunits:5e3,sellunits:8e3},{buyCurr:"USD",sellCurr:"INR",exchange:"BSE",buyunits:3e3,sellunits:18e4},{buyCurr:"USD",sellCurr:"JPY",exchange:"Japan",buyunits:32e3,sellunits:1e6},{buyCurr:"RUB",sellCurr:"INR",exchange:"Russia",buyunits:1e3,sellunits:1100},{buyCurr:"USD",sellCurr:"CNY",exchange:"China",buyunits:3e3,sellunits:18e3},{buyCurr:"CNY",sellCurr:"INR",exchange:"China",buyunits:1e4,sellunits:1e5}];

const output = input.reduce( ( sumObj, item ) => {
  const key = `${ item.buyCurr } ${ item.exchange }`;
  sumObj[ key ] = ( sumObj[ key ] || 0 ) + item.buyunits - item.sellunits;
  return sumObj;
}, { /* initial sumObj is going to be an empty object */ } );
console.log( output );

答案 1 :(得分:0)

我无法弄清楚如何在一次通过中完成所有操作,但是在函数中有一些逻辑,你可以运行它三次:

  • 购买交易
  • 出售交易
  • 合并交易

之后,您将获得总结的答案。不像一次通过那么好,但提供的功能足够灵活,可以处理所有3种情况: (您需要向下滚动才能看到完整的代码)

const transactions = [  
{buyCurr: "USD", sellCurr: "CAD", exchange: "Canada", buyunits: 1000, sellunits: 1200},  
{buyCurr: "EUR", sellCurr: "CAD", exchange: "Europe", buyunits: 10000, sellunits: 15000},  
{buyCurr: "GBP", sellCurr: "USD", exchange: "NYSE", buyunits: 5000, sellunits: 8000},  
{buyCurr: "USD", sellCurr: "INR", exchange: "BSE", buyunits: 3000, sellunits: 180000},  
{buyCurr: "USD", sellCurr: "JPY", exchange: "Japan", buyunits: 32000, sellunits: 1000000},  
{buyCurr: "RUB", sellCurr: "INR", exchange: "Russia", buyunits: 1000, sellunits: 1100},  
{buyCurr: "USD", sellCurr: "CNY", exchange: "China", buyunits: 3000, sellunits: 18000},  
{buyCurr: "CNY", sellCurr: "INR", exchange: "China", buyunits: 10000, sellunits: 100000},  
{buyCurr: "USD", sellCurr: "CAD", exchange: "Canada", buyunits: 1000, sellunits: 1200},  
{buyCurr: "EUR", sellCurr: "CAD", exchange: "Europe", buyunits: 10000, sellunits: 15000},  
{buyCurr: "GBP", sellCurr: "USD", exchange: "NYSE", buyunits: 5000, sellunits: 8000},  
{buyCurr: "USD", sellCurr: "INR", exchange: "BSE", buyunits: 3000, sellunits: 180000},  
{buyCurr: "USD", sellCurr: "JPY", exchange: "Japan", buyunits: 32000, sellunits: 1000000},  
{buyCurr: "RUB", sellCurr: "INR", exchange: "Russia", buyunits: 1000, sellunits: 1100},  
{buyCurr: "USD", sellCurr: "CNY", exchange: "China", buyunits: 3000, sellunits: 18000},  
{buyCurr: "CNY", sellCurr: "INR", exchange: "China", buyunits: 10000, sellunits: 100000},  
];

function positionSummary(transactions, summaryType) {
    const newList = _(transactions)
        .groupBy(summaryType === 'currency' ? summaryType : `${summaryType}Curr`)
        .map(action => {
            return _.groupBy(action, 'exchange');
        })
        .map(currency => {
            return _.map(currency, exchangeArray => {
                const currency = _.first(exchangeArray)[summaryType === 'currency' ? summaryType : `${summaryType}Curr`];
                const exchange = _.first(exchangeArray)['exchange'];
                let units = _(exchangeArray)
                    .sumBy(summaryType === 'currency' ? 'units' : `${summaryType}units`)
                    .valueOf();
                units = _.multiply(units, summaryType === 'sell' ? -1 : 1);

                return {
                    currency,
                    exchange,
                    units
                };
            });
        })
        .flatten()
        .valueOf();


    console.log(summaryType, newList);
    return newList;
}

const buyUnitSummary = positionSummary(transactions, 'buy');
const sellUnitSummary = positionSummary(transactions, 'sell');
const allUnitSummary = _.concat(buyUnitSummary, sellUnitSummary);
const finalPositionSummary = positionSummary(allUnitSummary, 'currency');
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>