GroupBy并以无点样式减少对象数组

问题描述 投票:3回答:5

我最近开始使用Ramda并试图找到一种无点的方法来编写一个减少对象数组的方法。

这是对象数组:

const someObj = [
    {
        name: 'A',
        city: 1,
        other: {
            playtime: 30
        }
    },
    {
        name: 'B',
        city: 2,
        other: {
            playtime: 20
        }
    },
    {
        name: 'c',
        city: 1,
        other: {
            playtime: 20
        }
    }
];

我正在尝试的是使用ramda在无党派风格中减少对象

{
    '1': {
        count: 2,
        avg_play_time: 20 + 30 / count
    },
    '2': {
        count: 1,
        avg_play_time: 20 / count
    }
}

我可以使用数组reduce方法来做,但不知道如何以ramda pointfree样式编写相同的方法。任何建议将不胜感激。

javascript reduce ramda.js
5个回答
4
投票

一种解决方案是做这样的事情:

// An optic to extract the nested playtime value
// Coupled with a `lift` operation which allows it to be applied over a collection
// Effectively A -> B => A[] -> B[]
const playtimes = R.lift(R.path(['other', 'playtime']))

R.pipe(
  // Group the provided array by the city value
  R.groupBy(R.prop('city')),
  // Return a body specification which computes each property based on the 
  // provided function value.
  R.map(R.applySpec({
    count: R.length,
    average: R.pipe(playtimes, R.mean)
  }))
)(someObj)

2
投票

Ramda还有另一个名为R.reduceBy的函数,它在qazxsw poi和qazxsw poi之间提供了一些东西,允许你用匹配的键将值折叠起来。

因此,您可以创建一个如下所示的数据类型,用于计算要平均的值。

reduce

然后使用groupBy将它们组合在一起。

const Avg = (count, val) => ({ count, val })
Avg.of = val => Avg(1, val)
Avg.concat = (a, b) => Avg(a.count + b.count, a.val + b.val)
Avg.getAverage = ({ count, val }) => val / count
Avg.empty = Avg(0, 0)

然后将R.reduceBy中的平均值拉成最终对象的形状。

const avgCities = R.reduceBy(
  (avg, a) => Avg.concat(avg, Avg.of(a.other.playtime)),
  Avg.empty,
  x => x.city
)

最后将两者连接在一起,将Avg映射到对象中的值。

const buildAvg = R.applySpec({
  count: x => x.count,
  avg_play_time: Avg.getAverage
})

2
投票

我会这样写,希望它有所帮助!

buildAvg
const fn = R.pipe(avgCities, R.map(buildAvg))
fn(someObj)

2
投票

这是另一个建议,使用const stats = R.pipe( R.groupBy(R.prop('city')), R.map( R.applySpec({ count: R.length, avg_play_time: R.pipe( R.map(R.path(['other', 'playtime'])), R.mean, ), }), ), ); const data = [ { name: 'A', city: 1, other: { playtime: 30 } }, { name: 'B', city: 2, other: { playtime: 20 } }, { name: 'c', city: 1, other: { playtime: 20 } }, ]; console.log('result', stats(data));在生成的对象的每个属性上映射<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>函数:

我的想法是使用reduceByapplySpec转换为这个对象:

someObj

然后,您可以在该对象的每个属性上映射getPlaytimeByCity函数:

{ 1: [30, 20],
  2: [20]}

stats
stats({ 1: [30, 20], 2: [20]});
// { 1: {count: 2, avg_play_time: 25}, 
//   2: {count: 1, avg_play_time: 20}}

1
投票

我喜欢到目前为止给出的所有其他答案。所以很自然地我想添加自己的。 ;-)

这是一个使用const someObj = [ { name: 'A', city: 1, other: { playtime: 30 }}, { name: 'B', city: 2, other: { playtime: 20 }}, { name: 'c', city: 1, other: { playtime: 20 }} ]; const city = prop('city'); const playtime = path(['other', 'playtime']); const stats = applySpec({count: length, avg_play_time: mean}); const collectPlaytime = useWith(flip(append), [identity, playtime]); const getPlaytimeByCity = reduceBy(collectPlaytime, [], city); console.log( map(stats, getPlaytimeByCity(someObj)) );来保持计数和平均值的运行轨迹的版本。如果您正在寻找中值或其他统计数据,这将无效,但给定计数,平均值和新值,我们可以直接计算新计数和平均值。这允许我们仅以每次迭代执行某些算术为代价迭代数据一次。

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>
<script>const {prop, path, useWith, flip, append, identity, applySpec, length, mean, reduceBy, map} = R;</script>
reduceBy

这不是免费的。虽然我是无点风格的忠实粉丝,但我只在它适用的时候使用它。我认为为它自己寻找它是一个错误。

请注意,可以很容易地修改Scott Christopher的答案以使用这种计算方法

© www.soinside.com 2019 - 2024. All rights reserved.