如何将逗号分隔的字符串转换为javascript中的数字数组?

时间:2022-04-08 00:17:31

I have a one-dimensional array of integer in JavaScript that I'd like to add data from comma separated string, Is there a simple way to do this?

我在JavaScript中有一个一维的整数数组,我想从逗号分隔的字符串中添加数据,有简单的方法吗?

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

e。g: var strVale = "130,235,342,124 ";

7 个解决方案

#1


26  

You can use split() to get string array from comma separated string. If you iterate and perform mathematical operation on element of string array then that element will be treated as number by run-time cast but still you have string array. To convert comma separated string int array see the edit.

可以使用split()从逗号分隔的字符串获取字符串数组。如果您在string数组的元素上迭代并执行数学运算,那么该元素将被运行时转换为number,但仍然有string数组。要转换逗号分隔的字符串int数组,请参阅编辑。

arr = strVale.split(',');

Live Demo

现场演示

var strVale = "130,235,342,124";
arr = strVale.split(',');
for(i=0; i < 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 to int Array In the above example the string are casted to numbers in expression but to get the int array from string array you need to convert it to number.

在上面的示例中,字符串被转换为表达式中的数字,但是要从字符串数组中获得int数组,需要将其转换为number。

var strVale = "130,235,342,124";
var strArr = strVale.split(',');
var intArr = [];
for(i=0; i < strArr.length; i++)
   intArr.push(parseInt(strArr[i]));

#2


14  

You can use the String split method to get the single numbers as an array of strings. Then convert them to numbers with the unary plus operator, the Number function or parseInt, and add them to your array:

可以使用String split方法将单个数字作为字符串数组。然后用一元加运算符、数字函数或parseInt将它们转换为数字,并将它们添加到数组中:

var arr = [1,2,3],
    strVale = "130,235,342,124 ";
var strings = strVale.split(",");
for (var i=0; i<strVale.length; i++)
    arr.push( + strings[i] );

Or, in one step, using Array map to convert them and applying them to one single push:

或者,在一个步骤中,使用数组映射来转换它们,并将它们应用到单个push:

arr.push.apply(arr, strVale.split(",").map(Number));

#3


12  

? "123,87,65".split(",").map(Number)
> [123, 87, 65]

Edit >>

编辑> >

Thanks to @NickN note! Using this approach, a filter is applicable if you by eg. want to exclude any non-numeric values:

由于@NickN注意!使用这种方法,如果您使用eg,可以使用过滤器。要排除任何非数字值:

? "123,87,hello,65".split(",").map(Number).filter(x => !isNaN(x)) 
> [123, 87, 65] 

#4


10  

just you need to use couple of methods for this, that's it!

你只需要用几个方法就行了!

var strVale = "130,235,342,124";
var resultArray = strVale.split(',').map(function(strVale){return Number(strVale);});

the output will be the array of numbers.

输出将是数字数组。

#5


2  

The split() method is used to split a string into an array of substrings, and returns the new array.

split()方法用于将字符串分割为一组子字符串,并返回新的数组。

Syntax:
  string.split(separator,limit)


arr =  strVale.split(',');

SEE HERE

在这里看到的

#6


2  

You can split and convert like

你可以像这样分开和转换。

 var strVale = "130,235,342,124 ";
 var intValArray=strVale.split(',');
 for(var i=0;i<intValArray.length;i++{
     intValArray[i]=parseInt(intValArray[i]);
}

Now you can use intValArray in you logic.

现在可以在逻辑中使用intValArray了。

#7


0  

Solution:

解决方案:

var answerInt = [];
var answerString = "1,2,3,4";
answerString.split(',').forEach(function (item) {
   answerInt.push(parseInt(item))
});

#1


26  

You can use split() to get string array from comma separated string. If you iterate and perform mathematical operation on element of string array then that element will be treated as number by run-time cast but still you have string array. To convert comma separated string int array see the edit.

可以使用split()从逗号分隔的字符串获取字符串数组。如果您在string数组的元素上迭代并执行数学运算,那么该元素将被运行时转换为number,但仍然有string数组。要转换逗号分隔的字符串int数组,请参阅编辑。

arr = strVale.split(',');

Live Demo

现场演示

var strVale = "130,235,342,124";
arr = strVale.split(',');
for(i=0; i < 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 to int Array In the above example the string are casted to numbers in expression but to get the int array from string array you need to convert it to number.

在上面的示例中,字符串被转换为表达式中的数字,但是要从字符串数组中获得int数组,需要将其转换为number。

var strVale = "130,235,342,124";
var strArr = strVale.split(',');
var intArr = [];
for(i=0; i < strArr.length; i++)
   intArr.push(parseInt(strArr[i]));

#2


14  

You can use the String split method to get the single numbers as an array of strings. Then convert them to numbers with the unary plus operator, the Number function or parseInt, and add them to your array:

可以使用String split方法将单个数字作为字符串数组。然后用一元加运算符、数字函数或parseInt将它们转换为数字,并将它们添加到数组中:

var arr = [1,2,3],
    strVale = "130,235,342,124 ";
var strings = strVale.split(",");
for (var i=0; i<strVale.length; i++)
    arr.push( + strings[i] );

Or, in one step, using Array map to convert them and applying them to one single push:

或者,在一个步骤中,使用数组映射来转换它们,并将它们应用到单个push:

arr.push.apply(arr, strVale.split(",").map(Number));

#3


12  

? "123,87,65".split(",").map(Number)
> [123, 87, 65]

Edit >>

编辑> >

Thanks to @NickN note! Using this approach, a filter is applicable if you by eg. want to exclude any non-numeric values:

由于@NickN注意!使用这种方法,如果您使用eg,可以使用过滤器。要排除任何非数字值:

? "123,87,hello,65".split(",").map(Number).filter(x => !isNaN(x)) 
> [123, 87, 65] 

#4


10  

just you need to use couple of methods for this, that's it!

你只需要用几个方法就行了!

var strVale = "130,235,342,124";
var resultArray = strVale.split(',').map(function(strVale){return Number(strVale);});

the output will be the array of numbers.

输出将是数字数组。

#5


2  

The split() method is used to split a string into an array of substrings, and returns the new array.

split()方法用于将字符串分割为一组子字符串,并返回新的数组。

Syntax:
  string.split(separator,limit)


arr =  strVale.split(',');

SEE HERE

在这里看到的

#6


2  

You can split and convert like

你可以像这样分开和转换。

 var strVale = "130,235,342,124 ";
 var intValArray=strVale.split(',');
 for(var i=0;i<intValArray.length;i++{
     intValArray[i]=parseInt(intValArray[i]);
}

Now you can use intValArray in you logic.

现在可以在逻辑中使用intValArray了。

#7


0  

Solution:

解决方案:

var answerInt = [];
var answerString = "1,2,3,4";
answerString.split(',').forEach(function (item) {
   answerInt.push(parseInt(item))
});