Ramda.js 按具有唯一值的键分组

Ramda.js group by key with unique values

I have the following ramda code 并且值不唯一

const transform = pipe(
  groupBy (prop ('category')),
  map (map (prop('type'))) //uniq is not working here
)

const data = [
  {"category": "a", "type" : "a_1"},
  {"category": "a", "type" : "a_1"},
  {"category": "a", "type" : "a_2"},
  {"category": "b", "type" : "b_1"},
  {"category": "b", "type" : "b_1"},
]
                                   
const result = transform(data)

console.log(result)
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>const { pipe, groupBy, prop, map } = R</script>

结果是:{"a": ["a_1", "a_1", "a_2"], "b": ["b_1", "b_1"]}
预期:{"a": ["a_1", "a_2"], "b": ["b_1"]}

坦克

映射 type 后,将项目通过管道传输到 R.uniq:

const { pipe, groupBy, prop, map, uniq } = R

const transform = pipe(
  groupBy(prop('category')),
  map(pipe(map(prop('type')), uniq))
)

const data = [{"category":"a","type":"a_1"},{"category":"a","type":"a_1"},{"category":"a","type":"a_2"},{"category":"b","type":"b_1"},{"category":"b","type":"b_1"}]

const result = transform(data)

console.log(result)
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

您也可以将 map(prop('type')) 替换为 R.pluck:

const { pipe, groupBy, prop, map, pluck, uniq } = R

const transform = pipe(
  groupBy(prop('category')),
  map(pipe(pluck('type'), uniq))
)

const data = [{"category":"a","type":"a_1"},{"category":"a","type":"a_1"},{"category":"a","type":"a_2"},{"category":"b","type":"b_1"},{"category":"b","type":"b_1"}]

const result = transform(data)

console.log(result)
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.28.0/ramda.min.js" integrity="sha512-t0vPcE8ynwIFovsylwUuLPIbdhDj6fav2prN9fEu/VYBupsmrmk9x43Hvnt+Mgn2h5YPSJOk7PMo9zIeGedD1A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>