Javascript javascript将字符串拆分为int数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8232776/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:14:33  来源:igfitidea点击:

javascript split string to array of int

javascriptjquery

提问by frenchie

I have a string that's on the page and from which I want an array of int.

我在页面上有一个字符串,我想要一个 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.

但是,ArrayData 变成了字符串数组。我怎样才能得到一个整数数组?请注意,HTML 中的某些元素可以等于 0。

Thanks.

谢谢。

采纳答案by Keith.Abramo

var ArrayData = $.map($('#TheData').text().split(','), function(value){
    return parseInt(value, 10);
    // or return +value; which handles float values as well
});

You can use $.mapto transform the array of strings to ints by calling parseInton each of the elements in the array

您可以$.map通过调用数组中的parseInt每个元素来将字符串数组转换为整数

回答by RightSaidFred

var ArrayData = $('#TheData').html().split(',').map( Number );

Add Array.prototype.map()to older browsers with the code from MDN.

Array.prototype.map()使用MDN 中的代码添加到旧浏览器。



You can use jQuery's $.map()in the same manner, though it won't work with $.prototype.map().

您可以$.map()以相同的方式使用 jQuery ,但它不适用于$.prototype.map().

var ArrayData = $.map( $('#TheData').html().split(','), Number );

回答by Tang Chanrith

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

回答by Daniel Santana

For those who want to solve this using pure Javascript, here is the way I did it:

对于那些想使用纯 Javascript 解决这个问题的人,我是这样做的:

const elementText = document.getElementById('TheData').innerText;
const numericList = Array.from(elementText.split(',')).map(item => Number(item));

For more information:

想要查询更多的信息:

1) 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.

1) getElementById: "Document 方法 getElementById() 返回一个 Element 对象,该对象表示其 id 属性与指定字符串匹配的元素。由于如果指定,元素 ID 必须是唯一的,因此它们是访问特定元素的有用方法迅速地。 (...)”。来源developer.mozilla.org

2) Array.from: "The Array.from() method creates a new, shallow-copied Array instance from an array-like or iterable object. (...)". Source: developer.mozilla.org

2) Array.from:“Array.from() 方法从类数组或可迭代对象创建一个新的、浅复制的 Array 实例。(...)”。来源developer.mozilla.org

回答by cpt.John

Here is a simple answer

这是一个简单的答案

`let x ="1,2,3,4";

`让 x ="1,2,3,4";

let result=x.split(",").map((e)=>parseInt(e));

let result=x.split(",").map((e)=>parseInt(e));

console.log(result);`

控制台日志(结果);`