I want to console.log()
my array like this without newlines
let myarr = [1,2,3,4,5];
myarr.forEach(e => console.log(e));
You could spread the array. Then all values are taken as parameters.
let array = [1, 2, 3, 4, 5]; console.log(...array);
You can use .reduce()
instead.
let myarr = [1,2,3,4,5]; const result = myarr.reduce((a, c) => `${a}${c}`, ''); console.log(result);
I hope this helps!
Why are you doing a separate console.log for every element in the array? You can just do:
console.log(myarr);
Or, if your array has objects in it and you want to see them all unrolled, you could do:
console.log(JSON.stringify(myarr));
You would have to stringify your output.
So either using something like JSON.stringify(myarr)
or
let string = '';
for(let i = 1; i < 6; i += 1) {
string += i + ' ';
}
console.log(string);
Edit: Btw, I'd suggest using camelCase when working with javascript. It's become a standard when defining variables and methods.
To answer the actual question, there are two possible answers:
process.stdout.write(msg)
from this answer Chrome JavaScript developer console: Is it possible to call console.log() without a newline?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.