我想在一行中将数字 5 6 7
返回到控制台,而不是字符串“5 6 7”。我想要数字,就像我输入 console.log(5, 6, 7)
一样。但是如果我有一个包含许多数字的数组,console.log([5, 6, 7].join(' '))
它会返回一个字符串。
最佳答案
不完全清楚你在问什么,但如果你使用 ES6,你可以使用 spread operator :
const myArray = [ 5, 6, 7 ];
console.log( ...myArray ); //use the es6 spread operator to turn the array into args
否则你可以使用.apply()
像这样:
var myArray = [ 5, 6, 7 ];
console.log.apply( console, myArray ); //use myArray as the arguments
关于javascript - 如何在javascript中连接多个数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46390154/