最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What is the Ramda equivalent of this Lodash uniqBy call? - Stack Overflow

programmeradmin3浏览0评论
const data = [
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1001]},
{id: "001", failedReason: [1002]},
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1000, 1003]},
{id: "002", failedReason: [1000]}
];

Given this data set if I wanted to return an array of objects with unique id values only then I could use Lodash to simply call: _.uniqBy(data, 'id')

I know that with this method only the first occurrence of an element/match is kept.

The above would return:

[ { id: '001', failedReason: [ 1000 ] },
  { id: '002', failedReason: [ 1000 ] } ]

I'm brand new to Ramda. What is the Ramda one- liner equivalent to this?

R.uniqBy(???, data)

const data = [
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1001]},
{id: "001", failedReason: [1002]},
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1000, 1003]},
{id: "002", failedReason: [1000]}
];

Given this data set if I wanted to return an array of objects with unique id values only then I could use Lodash to simply call: _.uniqBy(data, 'id')

I know that with this method only the first occurrence of an element/match is kept.

The above would return:

[ { id: '001', failedReason: [ 1000 ] },
  { id: '002', failedReason: [ 1000 ] } ]

I'm brand new to Ramda. What is the Ramda one- liner equivalent to this?

R.uniqBy(???, data)

Share Improve this question edited May 9, 2019 at 0:29 skålfyfan asked May 9, 2019 at 0:15 skålfyfanskålfyfan 5,3415 gold badges43 silver badges62 bronze badges 1
  • I know nothing about Ramda, but looking at the documentation it appears you need a function that takes each element returns the id. – Heretic Monkey Commented May 9, 2019 at 0:21
Add a ment  | 

2 Answers 2

Reset to default 6

Ramda doesn't do the lodash style of allowing a string to serve as an alternative for a function to get the named property of an object. So just pass a function:

const data = [
  {id: "001", failedReason: [1000]},
  {id: "001", failedReason: [1001]},
  {id: "001", failedReason: [1002]},
  {id: "001", failedReason: [1000]},
  {id: "001", failedReason: [1000, 1003]},
  {id: "002", failedReason: [1000]}
];

console.log(uniqBy(prop('id'), data))
<script src="https://bundle.run/[email protected]"></script><script>
const {uniqBy, prop} = ramda    </script>

Just return the id of each element:

const data = [
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1001]},
{id: "001", failedReason: [1002]},
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1000, 1003]},
{id: "002", failedReason: [1000]}
];
const res = R.uniqBy(({ id }) => id, data);
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src="//cdnjs.cloudflare./ajax/libs/ramda/0.25.0/ramda.min.js"></script>

You could also use the Ramda prop function:

const data = [
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1001]},
{id: "001", failedReason: [1002]},
{id: "001", failedReason: [1000]},
{id: "001", failedReason: [1000, 1003]},
{id: "002", failedReason: [1000]}
];
const res = R.uniqBy(R.prop("id"), data);
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src="//cdnjs.cloudflare./ajax/libs/ramda/0.25.0/ramda.min.js"></script>

发布评论

评论列表(0)

  1. 暂无评论