项目需求
统计数组中每个2_
0.500000_0.800000_1.000000中第一"_"前数字的个数,并生成饼图。
var alarmvalue = ["2_0.500000_0.800000_1.000000;3_0.500000_0.800000_0.757255;6_0.500000_0.800000_1.000000;8_0.500000_0.800000_0.920000", "2_0.500000_0.800000_1.000000;3_0.500000_0.800000_0.609366","0_0.500000_0.800000_0.573164;2_0.500000_0.800000_1.000000;3_0.500000_0.800000_0.702832;6_0.500000_0.800000_1.000000;8_0.500000_0.800000_1.000000"]
分离数组
var nArr = []; for (var j = 0; j < alarmvalue.length; j++) { var alarmStr = alarmvalue[j].split(";"); for (var i = 0; i < alarmStr.length; i++) { nArr.push(alarmStr[i].split("_")[0]) } }
汇总元素出现的个数
function getRepeatNum(arr) { return arr.reduce(function (prev, next) { prev[next] = (prev[next] + 1) || 1; return prev; }, {}); }
计算某个元素出现的个数
var dataList = getRepeatNum(nArr); function getDataList(dataList, num) { if (dataList[num] == undefined) { return 0; } else { return dataList[num]; } }
@lockdata.cn