我有一个包含以下数据的字符串:
6,11,32,110,235,369,640,1005,1436,2063,3057,4618,6444,9822,15468,20434,24126,27387,29459,31056,31982,32040,31233,29224,27342,26662,26956,27912,28999,28965,27826,25579,25722,24826,24605,24304,23464,23708,24099,24357,24237,24401,24344,23586,22380,21004,17287,14747,13076,12555,12144,11009,10950,10871,10824,10577,10527,10475,10421,10358,10295,10104
基本上我是从文件中读取这个字符串,我想把它传递给高图中的系列数据。当我做数据时:(上面提到的字符串),没有数据显示,可能因为它是一个字符串(即使所有内容都被声明为var)。但是当我自己声明一个数据数组时
var series = [6,11,32,110,235,369,640,1005,1436,2063,3057,4618,6444,9822,15468,20434,24126,27387,29459,31056,31982,32040,31233,29224,27342,26662,26956,27912,28999,28965,27826,25579,25722,24826,24605,24304,23464,23708,24099,24357,24237,24401,24344,23586,22380,21004,17287,14747,13076,12555,12144,11009,10950,10871,10824,10577,10527,10475,10421,10358,10295,10104];
它将数据显示在图表上。所以请告诉我如何将我正在从文件中读取的字符串转换为我声明的数据数组。
答案 0 :(得分:1)
使用.split
方法:
> "6,11,32,110,235,369".split(',')
["6", "11", "32", "110", "235", "369"]
如果你想让他们成为一个数字:
> "6,11,32,110,235,369".split(',').map(Number)
[6, 11, 32, 110, 235, 369]
答案 1 :(得分:1)
将字符串等于变量,然后使用split()
方法。
string.split(',');
答案 2 :(得分:0)
如果Highcharts只接受整数数组,则可以将字符串拆分为数组,然后使用map
将字符串转换为整数或浮点数:
var arr = str.split(',').map(function (el) {
return parseInt(el, 10);
});
答案 3 :(得分:-1)
让我们看一下下面的例子。您将更好地了解将字符串转换为数组的整个过程。您可以复制并粘贴代码以查看其是否有效。
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display the array values after the split.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var strValue = "How are you doing today?";
var StrToArray = str.split("");
document.getElementById("demo").innerHTML=res;
}
</script>
</body>
</html>