Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
like image 428
Armeen Harwood Avatar asked May 14 '15 04:05

Armeen Harwood


People also ask

How do you use concatenate in an array?

Definition and UsageThe concat() method concatenates (joins) two or more arrays. The concat() method returns a new array, containing the joined arrays. The concat() method does not change the existing arrays.

How do you concatenate arrays in an array?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.

How do you concatenate elements in an array in C++?

The recommended solution is to use the std::copy from the <algorithm> header to concatenate two arrays. The idea is to allocate memory large enough to store all values of both arrays, then copy the values into the new array with std::copy .


1 Answers

Use array join method.Join joins the elements of an array into a string, and returns the string. The default separator is comma (,). Here the separator should be an empty string.

var  x = [31,31,3,1].join("");

EDIT: To get the result as numeric

const  x = +[31,31,3,1].join("");

or

const  x = Number([31,31,3,1].join(""));
like image 83
Gilsha Avatar answered Oct 29 '22 17:10

Gilsha