简体   繁体   English

类型错误:无法读取未定义的属性“forEach”,javascript

[英]TypeError: Cannot read property 'forEach' of undefined , javascript

trying to go through a dictionary and get values and get each of the array values in it to manipluate them but get this error, at first I thought it might be because I forgot the semi-colon at the line that defines the dict but the error stays.试图通过字典 go 并获取值并获取其中的每个数组值以操纵它们但得到此错误,起初我认为这可能是因为我忘记了定义字典的行的分号但错误停留。



 getPieChartSeries(logsList){
   let devidedLogs = this.divideLogsByExitCode(logsList);
   console.log(devidedLogs);
   let pieChartSeries = [];
   Object.values(devidedLogs).array.forEach(element => {
     pieChartSeries.push(this.getPrecentageOfLogType(element,logsList))
   });
   console.log(pieChartSeries)
 }

 getPrecentageOfLogType(logsList,logsOfTypeList){
   let numOflogs = logsList.length
   let numOflogsOfType = logsOfTypeList.length
   let precentageOfLogType = Math.round((numOflogsOfType  / numOflogs ) * 100)

   return precentageOfLogType
 }
 getCurrentTime(){
   var d = new Date();
   return d.toLocaleString()
 }

devidedLogs is分开的日志是



{failedFaults: Array(0), failedProbesLogs: Array(1), failedRollbackLogs: Array(0), rollbackedLogs: Array(0), selfHealedLogs: Array(3)}failedFaults: []failedProbesLogs: [{…}]failedRollbackLogs: []rollbackedLogs: []selfHealedLogs: (3) [{…}, {…}, {…}]__proto__: Object

I think you are iterating on the wrong variable.我认为您正在迭代错误的变量。

Already .values() return the array, you don't have to add .array again after that. .values()已经返回数组,之后您不必再次添加.array

So your code should be updated to the below lines:因此,您的代码应更新为以下几行:

getPieChartSeries(logsList){
 let devidedLogs = this.divideLogsByExitCode(logsList);
 console.log(devidedLogs);
 let pieChartSeries = [];
 Object.values(devidedLogs).forEach(element => {
 pieChartSeries.push(this.getPrecentageOfLogType(element,logsList))
 });
 console.log(pieChartSeries)
}

Object.values(devidedLogs) will return an Array of all Object values, equivalently, you can use Object.keys(devidedLogs) to obtain the keys or Object.entries(devidedLogs) for an Array of key-value tuples. Object.values(devidedLogs) will return an Array of all Object values, equivalently, you can use Object.keys(devidedLogs) to obtain the keys or Object.entries(devidedLogs) for an Array of key-value tuples.

Check also the docs of the Object class:)还要检查 Object class 的文档:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型错误:无法读取未定义 Javascript 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined Javascript javascript UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'forEach' - javascript UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined 错误:TypeError:无法读取未定义的属性“forEach” - Error : TypeError: Cannot read property 'forEach' of undefined 类型错误:无法读取未定义的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined TypeError:无法读取未定义 React 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined React Javascript forEach - TypeError:无法设置未定义的属性 - Javascript forEach - TypeError: Cannot set property of undefined forEach 数组出现错误类型错误:无法读取未定义的属性“forEach” - forEach array getting error TypeError: Cannot read property 'forEach' of undefined 无法读取未定义错误 JavaScript 的属性“forEach” - Cannot read property 'forEach' of undefined error JavaScript 无法读取未定义的属性“forEach” - Firebase 和 Javascript - Cannot read property "forEach" of undefined - Firebase and Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM