jquery - How to convert comma separated string into numeric array in javascript -


i have one-dimensional array of integer in javascript i'd add data comma separated string, there simple way this?

e.g : var strvale = "130,235,342,124 ";

you can use split() string array comma separated string. if iterate , perform mathematical operation on element of string array element treated number run-time cast still have string array. convert comma separated string int array see edit.

arr = strvale.split(','); 

live demo

var strvale = "130,235,342,124"; arr = strvale.split(','); for(i=0; < arr.length; i++)     console.log(arr[i] + " * 2 = " + (arr[i])*2); 

output

130 * 2 = 260 235 * 2 = 470 342 * 2 = 684 124 * 2 = 248 

edit, comma separated string int array in above example string casted numbers in expression int array string array need convert number.

var strvale = "130,235,342,124"; var strarr = strvale.split(','); var intarr = []; for(i=0; < strarr.length; i++)    intarr.push(parseint(strarr[i])); 

Comments