簡體   English   中英

使用Ramda對數組的項進行分組和變換

[英]Using Ramda to group and transform items of an array

我需要按batchNumber將數組中的項目batchNumber並對其值求和。

目前,我使用的是ramda,但我可以分組但不能轉換結果。

您能給我一個使用ramda.js的例子嗎?

現場示例

const input = [
    {
        batchNumber: 'a',
        scrapAmount: 5
    },
    {
        batchNumber: 'a',
        scrapAmount: 10
    },
    {
        batchNumber: 'b',
        scrapAmount: 1
    },
    {
        batchNumber: 'b',
        scrapAmount: 2
    },
    {
        scrapAmount: 7
    },
    {
        scrapAmount: 3
    }
]

const byBatchNumber = R.groupBy((batch) => batch.batchNumber);


console.log(byBatchNumber(input))

/* result wanted

const output = [
    {
        batchNumber: 'a',
        scrapAmount: 15
    },
    {
        batchNumber: 'b',
        scrapAmount: 3
    },
    {
        batchNumber: undefined,
        scrapAmount: 10
    },
]

*/

groupWith()通過檢查batchNumber是平等eqProps() 然后map()每個子mergeWithKey() ,將mergeWithKey()應用於所有對象,並add() scrapAmount字段的值:

 const { compose, groupWith, eqProps, map, apply, mergeWithKey, add } = R; const input = [{"batchNumber":"a","scrapAmount":5},{"batchNumber":"a","scrapAmount":10},{"batchNumber":"b","scrapAmount":1},{"batchNumber":"b","scrapAmount":2},{"scrapAmount":7},{"scrapAmount":3}] const byBatchNumber = compose( map(apply(mergeWithKey((k, l, r) => k === 'scrapAmount' ? add(l, r) : r))), groupWith(eqProps('batchNumber')) ) console.log(byBatchNumber(input)) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> 

一個相當簡單的版本如下:

 const {pipe, groupBy, prop, map, pluck, sum} = R; const input = [ {batchNumber: 'a', scrapAmount: 5}, {batchNumber: 'a', scrapAmount: 10}, {batchNumber: 'b', scrapAmount: 1}, {batchNumber: 'b', scrapAmount: 2}, {scrapAmount: 7}, {scrapAmount: 3} ] const totalScrap = pipe( groupBy(prop('batchNumber')), map(pluck('scrapAmount')), map(sum) ) console.log(totalScrap(input)) 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script> 

首先請注意,這將R.groupBy((batch) => batch.batchNumber)簡化為R.groupBy(R.prop('batchNumber')) 它具有相同的功能,只是表達更為簡潔。

這是我所能做的,因為我認為這是我通常所做的工作中最有用的輸出格式,例如:

{"a": 15, "b": 3, "undefined": 10}

但是,重新讀取所需的輸出后,可能需要再執行兩個步驟:

 const {pipe, groupBy, prop, map, pluck, sum, toPairs, zipObj} = R; const input = [ {batchNumber: 'a', scrapAmount: 5}, {batchNumber: 'a', scrapAmount: 10}, {batchNumber: 'b', scrapAmount: 1}, {batchNumber: 'b', scrapAmount: 2}, {scrapAmount: 7}, {scrapAmount: 3} ] const totalScrap = pipe( groupBy(prop('batchNumber')), map(pluck('scrapAmount')), map(sum), toPairs, map(zipObj(['batchNumber', 'scrapAmount'])) ) console.log(totalScrap(input)) 
 <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.js"></script> 

這不做的一件事是生成一個具有batchNumber: undefined的項,而是返回一個帶有batchNumber: "undefined" (字符串)的項。雖然可以解決,但這是一個丑陋的步驟,而且我看不到真正的收益。 如果您的解決方案的值為"undefined"那么可能的解決方案將失敗。 如果這確實是一個阻止因素,那么您顯然可以在該管道的最后一步之前進行處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM