[英]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.