[英]How i can print this in one?
我想知道,如何用美元符號 ($) 分隔每個單詞並在最后用這個符號將其寫在一行中?我試圖將“”更改為 $ 並替換 - 沒有成功。 在此示例中,它在每個單詞之前打印 $ 但仍在一列中:/
class Add { constructor(...words) { this.words = words; } print() { var a = this.words; for (let v of a) { v = "$" + v; console.log(v); } } } var x = new Add("hehe", "hoho", "haha", "hihi", "huhu"); var y = new Add("this", "is", "awesome"); var z = new Add("lorem", "ipsum", "dolor", "sit", "amet", "consectetur", "adipiscing", "elit"); x.print(); y.print(); z.print();
除非你需要 object 做其他事情,否則將字符串放在數組中並使用array.join()
和'$'
作為連接參數,然后連接?
到最后。
工作片段:
let x = ["hehe", "hoho", "haha", "hihi", "huhu"]; console.log(`${x.join('$')}?`);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.