简体   繁体   English

"如何将一组数字加入到 1 个串联数字中?"

[英]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.

相关问题 如何在JavaScript中对串联数组进行排序? - How do I sort a concatenated array in JavaScript? 我如何在数字数组中找到重复项+计数器如何在内部重复每个数字 - How can i find the duplicates in array of numbers + the counter how each number is repeated inside 如何清除在计算器中输入的显示屏上的第一个数字,以便第二个数字不会与第一个数字连接? - How can I clear the first number on the display entered in a calculator so that the second number does not get concatenated to the first one? 我如何通过从 16 个数字的洗牌数组中弹出一个数字并将其插入 HTML 来构建表格? - how can i build table by poping a number from shuffled array of 16 numbers and insert it into the HTML? 如何找到不在数组中的随机数? 想要遍历随机数直到找到一个 - How can I find a random number not located in an array? Want to loop through random numbers until one is found 插座商家问题,我该如何解决它只是对数组进行排序并计算数字对的数量? - socket Merchant problem, how can I solve it just sorting the array and counting the number of pairs flooring the numbers? 如何创建一个带有随机数的数组? - how can I create an array with random numbers? 如何在数组中调用一定范围的数字? - how can i call a range of numbers in an array? 如何在数组中找到两个等于目标数字的数字 - How do I find two numbers in an array that are equal to a target number 如何从数字列表中选择最小的数字? - How can I pick the lowest number from a list of numbers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM