Math.min with map returns NaN on 0 in collection
Math.min with map returns NaN on 0 in collection
我正在编写一段代码,我需要在其中绘制出集合中的最小值和最大值。它工作正常,直到我将 0 个值引入现在 returns NaN 的集合中。如果我 运行 没有 0 值的函数它将按预期工作。
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)));
因此,如果我们的值介于 2 - 0.00003 - 0 之间,则最小值应为 0,最大值应为 2。
现在,如果是 10 到 0.000000001,它会起作用,或者如果我们有 1 到 10000,它会起作用。
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);
下面我将添加一个可行的示例
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);
遇到此问题的任何人的解决方案参考@VLAZ 解释
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);
问题是 0 的值被评估为 falsy,因此转向回退,在该实例中是未定义的 属性,导致 NaN。解决方案是将两者调换。
遇到此问题的任何人的解决方案参考@VLAZ 解释
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);
问题是 0 的值被评估为 falsy,因此转向回退,在该实例中是未定义的 属性,导致 NaN。解决方案是将两者调换。
我正在编写一段代码,我需要在其中绘制出集合中的最小值和最大值。它工作正常,直到我将 0 个值引入现在 returns NaN 的集合中。如果我 运行 没有 0 值的函数它将按预期工作。
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)));
因此,如果我们的值介于 2 - 0.00003 - 0 之间,则最小值应为 0,最大值应为 2。
现在,如果是 10 到 0.000000001,它会起作用,或者如果我们有 1 到 10000,它会起作用。
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);
下面我将添加一个可行的示例
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);
遇到此问题的任何人的解决方案参考@VLAZ 解释
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);
问题是 0 的值被评估为 falsy,因此转向回退,在该实例中是未定义的 属性,导致 NaN。解决方案是将两者调换。
遇到此问题的任何人的解决方案参考@VLAZ 解释
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);
问题是 0 的值被评估为 falsy,因此转向回退,在该实例中是未定义的 属性,导致 NaN。解决方案是将两者调换。