【发布时间】:2022-01-05 19:14:43
【问题描述】:
我想在 div 中显示 findMax() 输出,但我遇到了这个问题:
'findMax' 没有定义 no-undef , 'dataset' 未定义 no-undef
有人建议我需要使用 useState 但我不知道如何使它工作,我将不胜感激!
const pricedata = {
datasets: [
{
backgroundColor: "#0000",
barPercentage: 2,
barThickness: 5,
data: [1, 10, 30, 7, 42, 12],
label: "Update in prices",
maxBarThickness: 10
},
{
backgroundColor: "#0000",
barPercentage: 2,
barThickness: 5,
data: [11, 70, 18, 17, 24, 12],
label: "Update in prices",
maxBarThickness: 10
}
]
};
function findMax(PRICES) {
if (!PRICES) {
return 0;
}
return Math.max(...PRICES);
}
pricedata.datasets.forEach((dataset) => {
dataset.maxPrice = findMax(dataset.data);
});
pricedata.datasets.forEach((dataset) => {
console.log('max price is', dataset.maxPrice);
});
return (
<div>{findMax(dataset.data)}</div>
【问题讨论】:
标签: reactjs function math return dataset