[英]get value if not null with ramda js
How i write the code as functional if i has the value like this 如果我有这样的值,我如何编写代码作为功能
const data = {
name: null,
email: null,
username: 'johndoo'
}
in this case if i write the function that return the display name i just wrote data.name || data.email || data.username
在这种情况下,如果我编写返回显示名称的函数,我只写了data.name || data.email || data.username
data.name || data.email || data.username
I try to use ramda like 我尝试使用ramda
const getName = R.prop('name')
const getEmail = R.prop('email')
const getUsername = R.prop('username')
const getDisplayName = getName || getEmail || getUsername
but doesn't work, how to write it in ramdajs 但是不起作用,如何在ramdajs中编写它
You need to use a logical or R.either
: 您需要使用逻辑或 R.either
:
const getDisplayName = R.either(getName, R.either(getEmail, getUsername))
Or more compact with R.reduce
: 或者更紧凑的R.reduce
:
const getDisplayName = R.reduce(R.either, R.isNil)([getName, getEmail, getUsername])
const data = { name: null, email: null, username: 'johndoo' } const getName = R.prop('name') const getEmail = R.prop('email') const getUsername = R.prop('username') const eitherList = R.reduce(R.either, R.isNil) const getDisplayName = eitherList([getName, getEmail, getUsername]) console.log(getDisplayName(data))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.