[英]Some in Ramda.js
I'm trying to do a something like.some in js, but with ramda.js.我正在尝试在 js 中做类似的事情,但使用 ramda.js。 And I can't understand how.
我不明白怎么做。 I have an array of objects, and I want to get a true/false if some of object has value property with array.length > 1.
我有一个对象数组,如果某些 object 的 value 属性具有 array.length > 1,我想得到一个真/假。
const features = [
{
name: "First name",
type: "First type",
value: ["First value 1", "First value 2"],
},
{
name: "Second name",
type: "Second type",
value: ["Second value 1"],
}
];
In vanilla.js it can be like:在 vanilla.js 中,它可以是:
features.some((f) => f.value.length > 1)
But I want to do it with Ramda.但我想用 Ramda 来做。 And try this, but it is not work:
试试这个,但它不起作用:
const isHasSomeValues = R.gt(R.length(R.prop('value')), 1);
console.log(R.any(isHasSomeValues)(features))
R.any excepts a predicate function, and isHasSomeValues
is actually a false
value: R.any 除了一个谓词 function,
isHasSomeValues
实际上是一个false
值:
const isHasSomeValues = R.gt(R.length(R.prop('value')), 1); console.log(isHasSomeValues);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
To create a function that is a combination of multiple functions you can use R.pipe or R.compose to perform a set of actions, where each action receives the result of the previous one. To create a function that is a combination of multiple functions you can use R.pipe or R.compose to perform a set of actions, where each action receives the result of the previous one. The first action (function) in the pipeline is called with the values passed into the pipe - an object in your case.
使用传递给 pipe 的值调用管道中的第一个操作(函数) - 在您的情况下为 object。
const { pipe, any, prop, length, gt, __ } = R const fn = any(pipe( prop('value'), // get the value array length, // get the length gt(__, 0), // check if the length is greater than 0 )) const features = [{"name":"First name","type":"First type","value":["First value 1","First value 2"]},{"name":"Second name","type":"Second type","value":["Second value 1"]}] const result = fn(features) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
There are other ways to combine functions in Ramda, one of them is currying.在 Ramda 中还有其他组合函数的方法,其中之一是柯里化。 All of Ramda's functions are curried , and all of them has a fixed arity (the number of parameters the function accepts).
Ramda 的所有函数都是curried ,并且它们都具有固定的数量(function 接受的参数数量)。 This means that if pass a single parameter to a function that requires 2, you'll get back a partially applied function.
这意味着如果将单个参数传递给需要 2 的 function,您将返回部分应用的 function。 Only when you'll supply a 2nd parameter, the function would return the result.
只有当您提供第二个参数时,function 才会返回结果。
In this case, R.any has an arity of 2. I pass it the predicate ( pipe(...)
), and get a new function.在这种情况下,R.any 的元数为 2。我将谓词(
pipe(...)
)传递给它,并得到一个新的 function。 Only when I supply the 2nd value (the array), I get the result.只有当我提供第二个值(数组)时,我才能得到结果。
R.any
is the Ramda equivalent for Array#some
. R.any
是Array#some
的 Ramda 等价物。 I'd probably approach this like is the prop value not empty?我可能会像道具值不为空一样接近这个? , as you are not really interested in how many items the value contains...
gt 0
is enough. ,因为您对值包含多少项并不真正感兴趣...
gt 0
就足够了。
const isValueEmpty = R.propSatisfies(R.isEmpty, 'value'); const fn = R.any( R.complement(isValueEmpty), ); const data = [ { name: 'First name', type: 'First type', value: ['First value 1', 'First value 2'], }, { name: 'Second name', type: 'Second type', value: ['Second value 1'], }, ]; console.log( fn(data), );
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.