I have an array of objects and I would like to test it to determine whether a property with a certain value exists (at least one occurrence) and for it to return a boolean value to indicate the result. I'm using the Ramda library and have been experimenting with the has function to try and achieve this, however this only returns a boolean on whether the actual property exists and not it's respective value.
const data = [
{
id: 10004,
name: 'Daniel',
age: 43,
sport: 'football'
},
{
id: 10005,
name: 'Tom',
age: 23,
sport: 'rugby'
},
{
id: 10006,
name: 'Lewis',
age: 32,
sport: 'football'
},
];
Checking the array of objects for sport: 'rugby' should return true and sport: 'tennis' should return false.
Any help will be greatly appreciated, thanks.
If you're looking for a Ramda solution, this would do fine:
R.filter(R.propEq('sport', 'rugby'))(data)
R.has, as you noted, just checks whether an object has the named property. R.propIs checks whether the property is of the given type. R.propEq tests whether the property exists and equals a given value, and the more generic R.propSatisfies checks whether the property value matches an arbitrary predicate.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With