jQuery 如何在javascript中将逗号分隔的字符串转换为数字数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16396124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert comma separated string into numeric array in javascript
提问by user2256371
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 ";
例如: var strVale = "130,235,342,124 ";
回答by Adil
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()从逗号分隔的字符串中获取字符串数组。如果您对字符串数组的元素进行迭代并执行数学运算,那么该元素将被运行时转换视为数字,但您仍然拥有字符串数组。要转换逗号分隔的字符串 int 数组,请参阅编辑。
arr = strVale.split(',');
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 ArrayIn 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 数组在上面的例子中,字符串在表达式中被转换为数字,但要从字符串数组中获取 int 数组,您需要将其转换为数字。
var strVale = "130,235,342,124";
var strArr = strVale.split(',');
var intArr = [];
for(i=0; i < strArr.length; i++)
intArr.push(parseInt(strArr[i]));
回答by Serge
? "123,87,65".split(",").map(Number)
> [123, 87, 65]
Edit>>
编辑>>
Thanks to @NickN& @connexoremarks! A filteris applicable if you by eg. want to exclude any non-numeric values:
感谢@NickN和@connexo 的评论! 如果您通过例如过滤器是适用的。想要排除任何非数字值:
?", ,0,,6, 45,x78,94c".split(",").filter(x => x.trim().length && !isNaN(x)).map(Number)
> [0, 6, 45]
回答by Bergi
You can use the String split
methodto get the single numbers as an array of strings. Then convert them to numberswith the unary plus operator, the Number
function or parseInt
, and add them to your array:
您可以使用Stringsplit
方法将单个数字作为字符串数组获取。然后使用一元加号运算符、Number
函数 or将它们转换为数字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 applyingthem to one single push
:
或者,在一个步骤中,使用Arraymap
转换它们并将它们应用到单个push
:
arr.push.apply(arr, strVale.split(",").map(Number));
回答by Sai Chaithanya
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.
输出将是数字数组。
回答by PSR
回答by Kamal Pundir
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。
回答by Ravi Gaur
Solution:
解决方案:
var answerInt = [];
var answerString = "1,2,3,4";
answerString.split(',').forEach(function (item) {
answerInt.push(parseInt(item))
});
回答by connexo
All of the given answers so far create a possibly unexpected result for a string like ",1,0,-1,, ,,2"
:
到目前为止,所有给定的答案都为如下字符串创建了一个可能出乎意料的结果",1,0,-1,, ,,2"
:
",1,0,-1,, ,,2".split(",").map(Number).filter(x => !isNaN(x))
// [0, 1, 0, -1, 0, 0, 0, 2]
To solve this, I've come up with the following fix:
为了解决这个问题,我想出了以下修复:
",1,0,-1,, ,,2".split(',').filter(x => x.trim() !== "").map(Number).filter(x => !isNaN(x))
// [1, 0, -1, 2]
Please note that due to
请注意,由于
isNaN("") // false!
and
和
isNaN(" ") // false
we cannot combine both filter steps.
我们不能结合两个过滤步骤。
回答by Dilip Borad
This is an easy and quick solution when the string value is proper with the comma(,).
当字符串值与逗号(,)正确时,这是一个简单快捷的解决方案。
But if the string is with the last character with the comma, Which makes a blank array element, and this is also removed extra spaces around it.
但是如果字符串的最后一个字符带有逗号,这会产生一个空白的数组元素,并且这也被删除了它周围的额外空格。
"123,234,345,"
“123,234,345,”
So I suggest using push()
所以我建议使用 push()
var arr = [], str="123,234,345,"
str.split(",").map(function(item){
if(item.trim()!=''){arr.push(item.trim())}
})