Reputation: 11
I'm trying to do a something like .some in js, but with 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.
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:
features.some((f) => f.value.length > 1)
But I want to do it with 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))
Upvotes: 1
Views: 1286
Reputation: 193348
R.any excepts a predicate function, and isHasSomeValues
is actually a false
value:
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. The first action (function) in the pipeline is called with the values passed into the pipe - an object in your case.
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. All of Ramda's functions are curried, and all of them has a fixed arity (the number of parameters the function accepts). This means that if pass a single parameter to a function that requires 2, you'll get back a partially applied function. Only when you'll supply a 2nd parameter, the function would return the result.
In this case, R.any has an arity of 2. I pass it the predicate (pipe(...)
), and get a new function. Only when I supply the 2nd value (the array), I get the result.
Upvotes: 2
Reputation: 14199
R.any
is the Ramda equivalent for Array#some
.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.
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>
Upvotes: 3