无法读取未定义问题的属性

时间:2020-06-16 11:47:23

标签: javascript reactjs object

所以我在javascript中有这个对象:

const systolicAndDiastolicAndPulseAverage = [
    {
      name:'Systolic Average',
      style:'#FFDA83',
      id:'right',
      category:'Systolic Average',
      data: [
        { day: 23, value: 100 },
        { day: 24, value: 110 },
        { day: 25, value: 115 },
        { day: 26, value: 112 },
        { day: 27, value: 108 },
        { day: 28, value: 95 },
        { day: 29, value: 99 },
        { day: 30, value: 89 },
        { day: 31, value: 124 },
      ]
    },
    {
      name:'Diastolic Average',
      style:'#EA1D75',
      id:'left',
      category:'Diastolic Average',
      data: [
        { day: 23, value: 61 },
        { day: 24, value: 65 },
        { day: 25, value: 82 },
        { day: 26, value: 74 },
        { day: 27, value: 69 },
        { day: 28, value: 59 },
        { day: 29, value: 67 },
        { day: 30, value: 71 },
        { day: 31, value: 74 },
      ]
    },
    {
      name:'Pulse Average',
      style:'#5FE3A1',
      category:'Pulse Average',
      data: [
        { day: 23, value: 80 },
        { day: 24, value: 83 },
        { day: 25, value: 65 },
        { day: 26, value: 72 },
        { day: 27, value: 79 },
        { day: 28, value: 93 },
        { day: 29, value: 96 },
        { day: 30, value: 91 },
        { day: 31, value: 46 },
      ]
    }
  ]

所以基本上我想做的是遍历里面的每个数组(有3个数组),每次我都会遍历里面的值,并检查值是否在两端之间,如果它是true,我将向计数器加1 。(例如,我首先进入收缩期平均值和内部的数据数组,迭代所有值并检查它们)

for(let i in systolicAndDiastolicAndPulseAverage){
            let dataParsed = systolicAndDiastolicAndPulseAverage[i].data;

            for(let j=0;j<=dataParsed.length;j++)
            {

                let currentValue = dataParsed[j].value
                if(currentValue.value <= 89){
                    lowerEnd++;
                }
                if(currentValue.value>=90 && currentValue.value <= 119){
                    lowMed++;
                }
                if(currentValue.value >= 120 && currentValue <=139){
                    highMed++;
                }
                else{
                    high++
                }
            }
        }

        console.log(lowerEnd)
        console.log(lowMed)
        console.log(highMed)
        console.log(high)

到目前为止,这是我的代码,它给了我这个错误,为什么我使用console.log在没有循环的情况下检查了逻辑及其工作原理,所以为什么不能在循环中工作,请帮忙

1 个答案:

答案 0 :(得分:0)

for(let i in systolicAndDiastolicAndPulseAverage){
  let dataParsed = systolicAndDiastolicAndPulseAverage[i].data;

// 1.不是<= cose [9] object.value,只是<< / p>

for(let j=0;j <  dataParsed.length;j++)
  {

// 2.让currentValue = dataParsed [j] .value为false尝试不使用.value

     let currentValue = dataParsed[j]
      if(currentValue.value <= 89){
          lowerEnd++;
      }
      if(currentValue.value>=90 && currentValue.value <= 119){
          lowMed++;
      }
      if(currentValue.value >= 120 && currentValue <=139){
          highMed++;
      }
      else{
          high++
      }
  }
}