Question: I have an string as this:

```
var a="12,332,22,212";

```

Now I split it to an array like this:

```
a=a.split(",");

```

Then 

```
a=["12","332","22","212"];

```

While I want to get an array like this:

```
a=[12,332,22,212];

```

Which means that the element of the array should be `Number`.

Now I have to iterate the array:

```
for(var i=0,len=a.length;i<len;i++){
  a[i]=parseFloat(a[i]);
}

```

Is there any alternative idea?

Answer: 
Another way would be to use [JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) to do the conversion, means you need to add the missing brackets.

```
var a="12,332,22,212"; 
a = "[" + a + "]"; 
a = JSON.parse(a); //[12, 332, 22, 212]

```
Here, use this code

```
for(var i=0; i<a.length;i++) a[i] = +a[i];

```

That would be the simplest way, to the best of my knowledge, you can also use array.map though

```
var result = a.map(function (x) { 
    return parseInt(x, 10); 
});

```

Or you can use

```
var result = a.map(function (x) {return parseFloat(x, 10);});

```

Note: credits to dheerosaur for the above code