簡體   English   中英

是否有一個優雅的 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]));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM