将JSON中的字符串数组替换为整数数组[关闭]

问题描述 投票:-7回答:1

请参阅下面的json以获取highcharts {“chart”:{“type”:“column”},“credits”:{“enabled”:true,“href”:“#”,“position”:{“align”: “right”,“verticalAlign”:“bottom”,“y”:0},“text”:“www.midasplus.com”},“series”:[{“color”:{“linearGradient”:{“x1 “:1,”x2“:0,”y1“:0,”y2“:1},”停止“:[[0,”#9a1919“],[”0.25“,”#9a1919“],[” 0.5“,”#9a7319“],[”0.75“,”#9a1919“],[1,”#9a1919“]]},”数据“:[” - 1.03“,” - 3.01“,” - 2.25“ ,“0.63”,“ - 1.07”,“1.14”,“ - 0.38”,“2.03”,“ - 2.07”,“ - 3.55”,“ - 3.99”,“ - 0.41”],“negativeColor”:{“ linearGradient“:{”x1“: - 1,”x2“:0,”y1“:0,”y2“: - 1},”停止“:[[0,”#199A19“],[”0.25“, “#199A19”],[“0.5”,“#00CC00”],[“0.75”,“#199A19”],[1,“#199A19”]]},“showInLegend”:“false”}],“ title“:{”text“:”Control Chart“},”xAxis“:{”categories“:[”2013年第3季度“,”2014年第4季度“,”2014年第1季度“,”2014年第2季度“,”2014年第3季度“,” 2014年第4季度“,”2015年第1季度“,2015年第2季度”,“2015年第3季度”,“2015年第4季度”,“2016年第1季度”,“2016年第2季度”]}}

在这里,我需要改变“数据”:[“-1.03”,“ - 3.01”,“ - 2.25”,“0.63”,“ - 1.07”,“1.14”,“ - 0.38”,“2.03”, “-2.07”,“ - 3.55”,“ - 3.99”,“ - 0.41”]使用javascript将字符串数组值转换为同一json中的整数数组。

javascript arrays json highcharts
1个回答
0
投票
const data = {"chart": {"type": "column"},"credits": {"enabled": true,"href": "#","position": {"align": "right","verticalAlign": "bottom","y": 0},"text": "www.midasplus.com"},"series": [{"color": {"linearGradient": {"x1": 1,"x2": 0,"y1": 0,"y2": 1},"stops": [[0,"#9a1919"],["0.25","#9a1919"],["0.5","#9a7319"],["0.75","#9a1919"],[1,"#9a1919"]]},"data": ["-1.03","-3.01","-2.25","0.63","-1.07","1.14","-0.38","2.03","-2.07","-3.55","-3.99","-0.41"],"negativeColor": {"linearGradient": {"x1": -1,"x2": 0,"y1": 0,"y2": -1},"stops": [[0,"#199A19"],["0.25","#199A19"],["0.5","#00CC00"],["0.75","#199A19"],[1,"#199A19"]]},"showInLegend": "false"}],"title": {"text": "Control Chart"},"xAxis": {"categories": ["Q3 2013","Q4 2013","Q1 2014","Q2 2014","Q3 2014","Q4 2014","Q1 2015","Q2 2015","Q3 2015","Q4 2015","Q1 2016","Q2 2016"]} }    
const floatArr = data.series[0].data.map(item => {
    return parseFloat(item)
})

如果你想将它们作为整数,请使用函数parseInt(item, 10)parseFloat(item)

© www.soinside.com 2019 - 2024. All rights reserved.