我有这样的东西:
const rhs = ['Foo.Bar', 'Two', 'Three'];
我想得到:
Array<Foo.Bar<Two<Three>>>
我有这个可以用:
const literalType = rhs.reduce((a,b) => {
return [a,'<',b].join('');
});
const withBraces = literalType.concat(
new Array(rhs.length).fill(null).join('>')
);
const finalVal = `Array<${withBraces}>`,
但我正在寻找比这更巧妙的东西。有没有人知道如何简化它?
最佳答案
您可以像下面这样使用“Array.reduce”来实现这一点
const rhs = ['Foo.Bar', 'Two', 'Three'];
let res = rhs.reduce((s, d, i) =>
(s += '<' + d
, i == rhs.length -1 && (s += ('>'.repeat(i+1)))
, s)
, 'Array')
console.log(res)
关于javascript - 创建嵌套数组类型的巧妙方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53348307/