admin管理员组文章数量:1136398
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>
I'm writing this:
var ArrayData = ($('#TheData').html()).split(',');
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
.
Thanks.
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>
I'm writing this:
var ArrayData = ($('#TheData').html()).split(',');
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
.
Thanks.
Share Improve this question edited Mar 20, 2021 at 6:46 Amir 1,3383 gold badges15 silver badges29 bronze badges asked Nov 22, 2011 at 19:44 frenchiefrenchie 51.9k117 gold badges319 silver badges525 bronze badges 1- possible duplicate of To convert all elements in an array to integer in javascript? – Felix Kling Commented Nov 22, 2011 at 19:49
6 Answers
Reset to default 72var ArrayData = $('#TheData').html().split(',').map( Number );
Add Array.prototype.map()
to older browsers with the code from MDN.
You can use jQuery's $.map()
in the same manner, though it won't work with $.prototype.map()
.
var ArrayData = $.map( $('#TheData').html().split(','), Number );
var ArrayData = $.map($('#TheData').text().split(','), function(value){
return parseInt(value, 10);
// or return +value; which handles float values as well
});
You can use $.map
to transform the array of strings to ints by calling parseInt
on each of the elements in the array
Pure Javascript solution:
const elementText = document.getElementById('divSourceID').innerText;
const numericList = elementText.split(',').map(Number);
For more information:
getElementById: "The Document method getElementById() returns an Element object representing the element whose id property matches the specified string. Since element IDs are required to be unique if specified, they're a useful way to get access to a specific element quickly. (...)". Source: developer.mozilla.org.
Array.prototype.map: "The map() method creates a new array populated with the results of calling a provided function on every element in the calling array". Source: developer.mozilla.org.
array.map(Number): This call means the first received argument will be automatically converted into number and results in the same as if you explicitly declare the arrow function:
const numericList = elementText.split(',').map(Number);
same result as:
const numericList = elementText.split(',').map(str => Number(str));
JIT: Special thanks to @Robbendebiene for the excellent code review, simplifying the previous code.
var ArrayData = $('#TheData').text().split(',').map(Number);
You can find more here:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Here is a simple answer
let x = "1,2,3,4";
let result = x.split(",").map((e) => parseInt(e));
console.log(result);
var ArrayData = $('#TheData').html().split(',').map( d => { return parseInt(d) });
Use map function after Split, in callback of map you can parse that Integer
本文标签: jqueryJavaScript split string to array of intStack Overflow
版权声明:本文标题:jquery - JavaScript split string to array of int - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736964412a1957854.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论