[英]How can I join an array of numbers into 1 concatenated number?
How do I join this array to give me expected output in as few steps as possible?如何加入这个数组以尽可能少的步骤给我预期的输出?
var x = [31,31,3,1]
//expected output: x = 313131;
Use array join<\/code> method.
使用数组
join<\/code>方法。
Join<\/code> joins the elements of an array into a string, and returns the string.
Join<\/code>将数组的元素连接成一个字符串,并返回该字符串。
The default separator is comma (,).
默认分隔符是逗号 (,)。 Here the separator should be an empty string.
这里的分隔符应该是一个空字符串。
var x = [31,31,3,1].join("");
Javascript join() will give you the expected output as string
. Javascript join()将为您提供预期的输出
string
。 If you want it as a number
, do this:如果您希望它作为一个
number
,请执行以下操作:
var x = [31,31,3,1];
var xAsString = x.join(''); // Results in a string
var xAsNumber = Number(x.join('')); // Results in a number, you can also use +(x.join(''))
I can't think of anything other than我想不出除了
+Function.call.apply(String.prototype.concat, x)
Your question asks for a number, most of the answers above are going to give you a string.您的问题需要一个数字,上面的大多数答案都会给您一个字符串。 You want something like this.
你想要这样的东西。
const number = Number([31,31,3,1].join(""));<\/code>
"
Try join() as follows尝试加入()如下
![]()
var x = [31,31,3,1] var y = x.join(''); alert(y);<\/code><\/pre>
"
Try below.下面试试。
var x = [31,31,3,1]
var teststring = x.join("");
This will work这将起作用
var x = [31,31,3,1];
var result = x.join("");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.