I would like to refactor the code below using Ramda. I want to compare the list of keys and values with the data that I receive from the server, if the data matches, the function run. To do this, I created a list of keys and values and using include()
I get a boolean
.
//example data
data = { fieldName: 'secretKey', fieldValue: 'ymt' }
const fieldNames = ['secretKey', 'activateCode']
const fieldValues = ['ymt', 'IdTYj']
if ( fieldNames.includes(data.fieldName) || fieldValues.includes(data.fieldValue)) {
yield put(isLoading(true));
}
Instead of include()
I used R.include
but I always get false
const field = R.equals( fieldNames, data.fieldName) // return false
Which method in Ramda is more suitable for comparing keys and values to return a boolean
?
I think you're looking for where
eg
const check = where({ username: either(equals('foo'), equals('FOO'))
, password: either(equals('bar'), equals('BAR'))});
check({username: 'foo', password: 'qux'}); // false
check({username: 'qux', password: 'bar'}); // false
check({username: 'foo', password: 'bar'}); // true
check({username: 'foo', password: 'BAR'}); // true
check({username: 'FOO', password: 'BAR'}); // true
I'd rather do something alike, You need to R.flip(R.includes)
to use it within your algorithm
const put = R.identity; const isLoading = R.identity; function* validate(data) { const keys = ['secretKey', 'activateCode']; const values = ['ymt', 'IdTYj']; const predicate = R.where({ fieldName: R.includes(R.__, keys), fieldValue: R.includes(R.__, values), }); if (predicate(data)) { yield put(isLoading(true)); } }; const iterator = validate({ fieldName: 'secretKey', fieldValue: 'ymt', }); console.log( iterator.next().value );
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.