【发布时间】:2020-10-17 09:49:15
【问题描述】:
我想做一些像下面这样的函数 xxx
const source = ['a','b','c']
const result = source.xxx(key => key + 'hello')
// or
const result = xxx(source,() => key => key + 'hello')
console.log(result)
// result
{
'a': 'ahello',
'b': 'bhello',
'c': 'chello'
}
我知道我可以编写如下代码来获取它
const source = ['a','b','c']
const xxx = (source: any[],callback) => {
let result = {}
source?.forEach(key => result = Object.assign({},result,{key: callback(key)})
return result
}
const result = xxx(source,(key) => key + 'hello')
但是是否有任何库或本机语法可以做到这一点而无需我自己编写?
【问题讨论】:
-
这个问题的“任何库”部分对于 SO 来说是题外话。图书馆推荐被特别称为题外话。
标签: typescript