是否有一个优雅的 javascript 等同于使用格式化的 python 代码?
Is there a elegant javascript equivalent of this python code using formatting?
是否有与此 python 代码等效的 javascript?我知道您可以使用 ``,但如何将它与 ... 展开运算符结合使用?
'{}{}{} {}{}{}-{}{}{}{}'.format(*array)
数组是长度为 10 的数字列表,预期的结果类似于
'123 456-7890'
您可以通过移动数组值来替换。
const
format = (pattern, [...array]) =>
pattern.replace(/\{\}/g, Array.prototype.shift.bind(array));
console.log(format('{}{}{} {}{}{}-{}{}{}{}', [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));
是否有与此 python 代码等效的 javascript?我知道您可以使用 ``,但如何将它与 ... 展开运算符结合使用?
'{}{}{} {}{}{}-{}{}{}{}'.format(*array)
数组是长度为 10 的数字列表,预期的结果类似于
'123 456-7890'
您可以通过移动数组值来替换。
const
format = (pattern, [...array]) =>
pattern.replace(/\{\}/g, Array.prototype.shift.bind(array));
console.log(format('{}{}{} {}{}{}-{}{}{}{}', [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]));