[英]Chrome JavaScript developer console: Is it possible to call console.log() without a newline?
[英]Is there anyway to console.log without a newline?
我想在沒有換行符的情況下像這樣console.log()
我的數組
let myarr = [1,2,3,4,5];
myarr.forEach(e => console.log(e));
你可以傳播陣列。 然后將所有值作為參數。
let array = [1, 2, 3, 4, 5]; console.log(...array);
您可以使用.reduce()
代替。
let myarr = [1,2,3,4,5]; const result = myarr.reduce((a, c) => `${a}${c}`, ''); console.log(result);
我希望這有幫助!
為什么要為數組中的每個元素都做一個單獨的 console.log? 你可以這樣做:
console.log(myarr);
或者,如果您的數組中有對象並且您希望看到它們全部展開,您可以執行以下操作:
console.log(JSON.stringify(myarr));
您必須對輸出進行字符串化。
所以要么使用像JSON.stringify(myarr)
類的東西,要么
let string = '';
for(let i = 1; i < 6; i += 1) {
string += i + ' ';
}
console.log(string);
編輯:順便說一句,我建議在使用 javascript 時使用camelCase。 它已成為定義變量和方法時的標准。
要回答實際問題,有兩種可能的答案:
process.stdout.write(msg)
Chrome JavaScript 開發人員控制台:是否可以在沒有換行符的情況下調用 console.log()?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.