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)
- 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
2 Answers
Reset to default 6Ramda 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>