How do I get result like this from a javascript array?

Hi,

I’m trying to do something like this with several methods unsuccessfully:


var array = "0, 1, 2, 3, 4, 5, 6, 7, 8, 9";

And I want the result like this:


"0123456789"

In ascending order, how do I achieve this?

Thanks

You can use a regular expression to achieve this.

var array = “0, 1, 2, 3, 4, 5, 6, 7, 8, 9”;
var new_array = array.replace(/,\s/g, “”);

This assumes that your original string is already in ascending order.

If it were an actual array that you start with, then your desired task would be simple and easy.


var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

Are you sure that that’s not what you’re wanting to start with?

I split data into an array to send through the server for greater performance. But I have problems to reassemble them.

Anyway, Ian’s method works in the test.

Hope this would help me.
Thanks

Then to help you with future occasions, JSON is the technique that you should be using for the easiest way to transmit and convert the data.

Hey Paul,

You’re right.

How about this array:


var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (var i = 0; i < 10; ++i) {
	console.log(array[i]); // 9
}

How do I get it as 0123456789

The join method would be most useful for that, using an empty string for the separator.