parseFloat() is not parsing all strings in my array. Javascript
I am making arrays out of columns in a table that I use to graph Flot charts. I start by working my way down the column and storing each column as its own array, then I check the column to the right and repeat.
Everything has been working great, except for this one tiny issue I am having where I am reading out the string values from the last column and parsing them into floats. All the values in last column parse to floats except for a few. Sometimes it's the 0th and 2nd element in the array, or sometimes the 0th and 1st.
Here's what it looks like when I console.log
the arrays:
The table also known as #maintable
:
As you can see in the last array the 0th and 2nd values in the array are returned as strings, but the rest are returned as floats.
Here is the code:
HTML:
<div id="chart_container" class="widget-body col-md-12" style="height: 453px;">
<section id="mainchart" class="col-md-12" style="height: 453px;"></section>
</div>
JS:
var slices = [];
var sliceNames, sliceNights, sliceNightsAvg, sliceRevs = [];
var sliceSet = [];
var colorPalette = [];
//Get number of columns by counting number of cells in the header row
var numOfColumns = $("#maintable tr th").length;
function getCellData(array, columnNum) {
$("#maintable tr td:nth-child(" + columnNum + ")").each(function (k, v) {
if(!array) {
array = [];
}
//Store the slice labels in array
array[k] = $(this).text();
//Remove any long whitespace from cell
array[k] = array[k].trim();
//Check if the data in the cell is a number or not
if (parseFloat(array[k]) % 1 === 0) {
//Filter the values
array[k] = array[k].replace("$", "");
array[k] = array[k].replace(",", "");
array[k] = array[k].replace("K", "000");
array[k] = array[k].replace("M", "000000");
//If there's a decimal
/*if (array[k].indexOf(".") !== -1) {
//Then parse as a float
array[k] = parseFloat(array[k]);
} else {
//Otherwise, parse as an integer
array[k] = parseInt(array[k]);
}*/
array[k] = parseFloat(array[k]);
}
});
return array;
}
//Get cell data
sliceNames = getCellData(sliceNames, 1);
sliceNights = getCellData(sliceNights, 2);
sliceNightsAvg = getCellData(sliceNightsAvg, 3);
sliceRevs = getCellData(sliceRevs, 4);
console.log(sliceNames);
console.log(sliceNights);
console.log(sliceNightsAvg);
console.log(sliceRevs);
//Loop through and create the slices
for (var i = 0; i < sliceNames.length; i++) {
if (!sliceSet) {
sliceSet = [];
}
//Setup the slices
var slice = {};
slice.label = sliceNames[i];
slice.data = sliceNights[i];
//Push the slice to the set of slices
sliceSet.push(slice);
}
//Plot the Pie chart
plotPieChart(sliceSet);
function plotPieChart(data) {
$.plot($("#mainchart"), data, {
series: {
pie: {
show: true,
innerRadius: 0.3,
label: {
show: true,
threshold: 0.01
}
},
grid: {
hoverable: true,
clickable: true
},
legend: {
show: true
}
}
});
}
Some time there is an invisible character in a string, please remove this character. Please check length of the string, if length is greater than visible characters then invisible character is existed. this character is not ASCII character so I am unable to write here. The character code is "u+200E".