[英]What is the Ramda equivalent of this Lodash uniqBy call?
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')
给定此数据集,如果我只想返回具有唯一
id
值的对象数组,则可以使用Lodash简单地调用: _.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. 我是Ramda的新手。 What is the Ramda one- liner equivalent to this?
Ramda单衬板等于什么?
R.uniqBy(???, data)
Just return the id
of each element: 只需返回每个元素的
id
:
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.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
You could also use the Ramda prop
function: 您还可以使用Ramda
prop
函数:
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.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
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. Ramda并没有采用lodash的样式,即允许字符串用作函数的替代方法来获取对象的命名属性。 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/ramda@0.26.1"></script><script> const {uniqBy, prop} = ramda </script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.