javascript将字符串拆分为int数组 [英] javascript split string to array of int
本文介绍了javascript将字符串拆分为int数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串在页面上,我想要一个int数组。
I have a string that's on the page and from which I want an array of int.
<div id="TheData">2,3,0,43,23,53</div>
我这样写:
var ArrayData = ($('#TheData').html()).split(',');
但是,ArrayData变成了一个字符串数组。如何获得一系列整数?请注意,HTML中的某些元素可以等于0.
However, ArrayData becomes an array of strings. How can I get an array of ints? Note that some of the elements in the HTML can be equal to 0.
谢谢。
推荐答案
var ArrayData = $.map($('#TheData').text().split(','), function(value){
return parseInt(value, 10);
// or return +value; which handles float values as well
});
您可以使用 $。map
进行转换通过在数组中的每个元素上调用 parseInt
来生成字符串数组
You can use $.map
to transform the array of strings to ints by calling parseInt
on each of the elements in the array
这篇关于javascript将字符串拆分为int数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文