如何将字符串数组转换为整数?用于HighChart.js数据处理
Got it, let's fix this step by step. The key issue here is that you're joining your string array into a single string first, which isn't what Highcharts needs—HighCharts requires an array of numeric values instead. Here's how to convert each element to an integer properly:
Step 1: Convert each string element to an integer
Your original gdv is an array of string-formatted numbers (like ["28248151.73", "42523366.46", ...]). We'll use the map() method to iterate over each element and convert it to an integer. You can choose different rounding behaviors based on your needs:
Option 1: Truncate decimal places (keep only the integer part)
Use parseInt (make sure to specify base 10 to avoid unexpected behavior) or Math.floor:
// Using parseInt var integerGDV = gdv.map(item => parseInt(item, 10)); // Or using Math.floor (same result for positive numbers) var integerGDV = gdv.map(item => Math.floor(parseFloat(item)));
Option 2: Round to the nearest integer
If you need to round the decimal values instead of truncating, use Math.round:
var integerGDV = gdv.map(item => Math.round(parseFloat(item)));
Step 2: Use the integer array in HighCharts
Now you have a proper array of integers that HighCharts can work with. Update your code to skip the join() call (since that turns the array into a single string, which is useless for the chart) and use the converted array directly:
$(function () { // Convert the string array to integers (choose your preferred method above) var integerGDV = gdv.map(item => Math.round(parseFloat(item))); console.log(integerGDV); // This will log an array of integers, e.g., [28248152, 42523366, ...] // Initialize your HighChart with the numeric array Highcharts.chart('container', { // Your chart configuration (title, axes, etc.) series: [{ data: integerGDV // Use the converted integer array here }] }); });
Quick Note
We use parseFloat first when using Math.floor or Math.round because the original values are strings with decimals—parseFloat correctly reads the full decimal value before we adjust it to an integer.
内容的提问来源于stack exchange,提问作者Wei Kang

