【问题标题】:display the output of math.max and math.min显示 math.max 和 math.min 的输出
【发布时间】: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


    【解决方案1】:

    代码的根级别没有“数据集”变量。 pricedata 变量包含一个名为 datasets 的数组。在您的代码中,当您调用 map 时,它会遍历所有数据集 - 调用每个单独的数据集 dataset。该变量名仅“限定”在 map() 函数内。

    您可以像这样在地图中构建 DIV:

    // call your findMax() on each element
    const divs = pricedata.datasets.forEach((dataset,i) => (<div key={i}>{findMax(dataset.data)}</div>))
    
    // using React, you'll need to surround the divs in a parent div
    return <>{divs}</>
    

    【讨论】:

    • 每个元素是什么意思?
    • 我的意思是数组的每一项。
    • 它说“意外的关键字'const'。”
    猜你喜欢
    • 1970-01-01
    • 2010-12-31
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2015-06-27
    • 1970-01-01
    • 2017-01-17
    相关资源
    最近更新 更多