【发布时间】:2019-02-12 17:12:50
【问题描述】:
我有一个将转换为字符串的数据数组。我已经创建了进行转换的函数。我的问题是编写此函数以使其更具可读性的最佳方法是什么?我不想有多个if/else 语句。
const data = [
"hello",
{name: "Bob"},
"and",
{name: "Fred"},
"How are you guys today?",
]
const isString = R.is(String)
const isObject = R.is(Object)
const getName = R.prop('name')
const toPureString = R.reduce(
(result, value) => {
if (isString(value)) {
return `${result}${value} `
}
if (isObject(value)) {
return `${result}${getName(value)}`
}
return result
}, "")
toPureString(data)
// hello Bob and Fred How are you guys today?
【问题讨论】:
标签: javascript functional-programming ramda.js