如何在jquery中将逗号分隔的字符串转换为数字(整数)数组

时间:2017-02-10 13:44:32

标签: javascript jquery arrays string

我需要将具有多个数字的字符串转换为jQuery中的数组。我的字符串如下:

var strVal = "901,235,342,421,345,745,544,324,445,123,232,986,345,678";

2 个答案:

答案 0 :(得分:2)

您可以将String.split','一起使用,然后将Array#mapNumber转化后的值

一起使用



var strVal = "901,235,342,421,345,745,544,324,445,123,232,986,345,678",
    numbers = strVal.split(',').map(Number);

console.log(numbers);

.as-console-wrapper { max-height: 100% !important; top: 0; }




答案 1 :(得分:1)

这个

你不需要jquery

var strVal = "901,235,342,421,345,745,544,324,445,123,232,986,345,678";
var result = strVal.split(",").map(x=>+x);
console.log(result);