I want to compare 2 values based on which I get the color for my legend. But unfortunately I cannot use ceil, floor or round the values as it will effect my result.
I have following color table with first value as point and rest 3 values are r g b values.
Basically I get point from backend which ca be 0.4607441262895224, 0.5500956769649571 and so on. I need to compare the point with first value in color table and give corresponding color. But I am facing issue in comparing , as currently I have given to Fixed(2) but it is not recommended.
"colors": [
[ 0.00, 255, 13, 186 ],
[ 0.25, 254, 4, 135 ],
[ 0.50, 73, 255, 35 ],
[ 0.75, 185, 116, 255 ],
[ 1.00, 32, 50, 255 ]
]
// main logic
const test = arraySet.find((ele) => {
// point is dynamic
const point = 0.388920938
// I cannot use toFixed(2) here, which need to be changed
// ele[0] is the first value in array 0.00, 0.25, 0.50 and so on
return point.toFixed(2) == ele[0].toFixed(2);}
);
CodePudding user response:
I suppose you want something on the lines of
Math.abs(point-ele[0]) < 0.01
replace 0.01
with a threshold relevant to your data, e.g., 1e-6
for 10^(-6)
CodePudding user response:
ToFixed will also be rounded
const toFixed=(num)=>Number(num.toString().match(/^\d (?:\.\d{0,2})?/));
CodePudding user response:
Try doing it like this
return point.toString().substr(0, ele.toString().length) == ele;