Ramda - 组合数组中具有相同键值的重复对象

Ramda - combine duplicates of objects in array with same key value

我有一个对象数组,如果它们具有相同的名称,我想合并它们,并将它们的 feederTeam 添加到一个数组中。

数组如下所示:

const TEAMS = [{
    name: 'Liverpool',
    id: '1',
    feederTeam: 'Tranmere'
}, {
    name: 'Liverpool',
    id: '2',
    feederTeam: 'Stockport'
}, {
    name: 'Man Utd',
    feederTeam: 'Bla',
    id: '3',
}, {
    name: 'Liverpool',
    id: '6',
    feederTeam: 'Oldham'
}]

我想使用 Ramda 将其转换成这样:

[{
    name: 'Liverpool',
    feederTeam: ['Tranmere', 'Stockport', 'Oldham']
}, {
    name: 'Man Utd',
    feederTeam: ['Bla'],
}]

我试过很多方法都没有成功。这是我最近的尝试。

R.forEach((team => R.filter(R.propEq('name', team.name), TEAMS)), TEAMS); 

因为您想将多个元素组合在一起,请使用 R.groupBy。然后从带R.values的组的对象中得到一个数组,并将每个组映射到需要的形式:

const { pipe, groupBy, prop, values, map, applySpec, nth } = R

const fn = pipe(
  groupBy(prop('name')), // group object by the name property
  values, // get an array of groups
  map(applySpec({ // map each group to an object
    name: pipe(nth(0), prop('name')), // take the name from the 1st object in the group
    feederTeam: map(prop('feederTeam')), // collect all feedTeam props to an array
  })),
)

const teams = [{"name":"Liverpool","id":"1","feederTeam":"Tranmere"},{"name":"Liverpool","id":"2","feederTeam":"Stockport"},{"name":"Man Utd","feederTeam":"Bla","id":"3"},{"name":"Liverpool","id":"6","feederTeam":"Oldham"}]

const result = fn(teams)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>

一个细微的变化是用 R.mapObjIndexed 映射组的对象。在这种情况下,name 键(是提供给 R.applySpec 的第二个参数)。将组映射到对象后,将结果转换为数组 R.values:

const { pipe, groupBy, prop, values, mapObjIndexed, applySpec, map, nthArg } = R

const fn = pipe(
  groupBy(prop('name')), // group object by the name property
  mapObjIndexed(applySpec({ // map each group to an object
    name: nthArg(1), // take the name from key (2nd arg)
    feederTeam: map(prop('feederTeam')), // collect all feedTeam props to an array
  })),
  values, // convert to an array of objects
)

const teams = [{"name":"Liverpool","id":"1","feederTeam":"Tranmere"},{"name":"Liverpool","id":"2","feederTeam":"Stockport"},{"name":"Man Utd","feederTeam":"Bla","id":"3"},{"name":"Liverpool","id":"6","feederTeam":"Oldham"}]

const result = fn(teams)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>