I'm working on a piece of code where I need to map out Min and Max values within the collection. It worked fine until I introduced 0 values into the collection which now returns NaN. If i run the function without the 0 values it will work as intended.
const minVal = Math.min(...this.wells.map(d => Number(d.valueText) || Number(d.value)));
const maxVal = Math.max(...this.wells.map(d => Number(d.valueText) || Number(d.value)));
So if we have values from 2 - 0.00003 - 0 the minimum value should be 0 and the maximum value should be 2.
now if it was lets say 10 to 0.000000001 it will work or if we will have 1 to 10000 it will work.
let wells = [];
wells.push({
posCol: 0,
posRow: 0,
valueText: 2
});
wells.push({
posCol: 1,
posRow: 0,
valueText: 4
});
wells.push({
posCol: 2,
posRow: 0,
valueText: 0
});
const minVal = Math.min(...wells.map(d => Number(d.valueText) || Number(d.value)));
const maxVal = Math.max(...wells.map(d => Number(d.valueText) || Number(d.value)));
console.log(minVal);
console.log(maxVal);
Below I'll add an example that will work
let wells = [];
wells.push({
posCol: 0,
posRow: 0,
valueText: 2
});
wells.push({
posCol: 1,
posRow: 0,
valueText: 4
});
wells.push({
posCol: 2,
posRow: 0,
valueText: 1
});
const minVal = Math.min(...wells.map(d => Number(d.valueText) || Number(d.value)));
const maxVal = Math.max(...wells.map(d => Number(d.valueText) || Number(d.value)));
console.log(minVal);
console.log(maxVal);
SOLUTION FOR ANYONE WHO COMES ACROSS THIS ISSUE Ref @VLAZ Explanation
let wells = [];
wells.push({
posCol: 0,
posRow: 0,
valueText: 2
});
wells.push({
posCol: 1,
posRow: 0,
valueText: 4
});
wells.push({
posCol: 2,
posRow: 0,
valueText: 0
});
const minVal = Math.min(...wells.map(d => Number(d.value) || Number(d.valueText)));
const maxVal = Math.max(...wells.map(d => Number(d.value) || Number(d.valueText)));
console.log(minVal);
console.log(maxVal);
The issue was that value of 0 was evaluated as falsy and as such went to a fallback that in that instance was an undefined property which resulted in NaN. Solution was to switch the two around.