javascript - Ramda - 将数组中具有相同键值的对象的重复项组合起来

标签 javascript ecmascript-6 ramda.js

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

数组看起来像这样:

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>

关于javascript - Ramda - 将数组中具有相同键值的对象的重复项组合起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63757774/

相关文章:

javascript - JavaScript ES6 中的解构对象函数

javascript - 使用 Ramda 返回应用函数数组

javascript - Monad的功能组合…不起作用

javascript - 如何在React上正确开发分页?

Javascript 启用已禁用的文本框

javascript - 就地编辑

javascript - php - 将 ajax post 的值返回给 javascript

javascript - 为什么箭头函数可以在 ReactJS 类上使用,但不能在纯 ES6 类上使用?

javascript - 从稍微嵌套的数组中删除重复项后保留对象的所有键

javascript - 在 IE7 中模拟 localStorage 的问题