javascript - 类型 '{ [key: number]: any[]; 上不存在属性 'length' }'。在状态变量中的 React/Angular
问题描述
当我尝试循环状态数据时出现以下错误
类型 '{ [key: number]: any[]; 上不存在属性 'length' }'。
国家声明
export interface State {
resultData:{ [key: number]: any[] };
}
类声明
export class AccountComp extends React.PureComponent<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
resultData: {},
};
}
update function showing issue in second for loop .
Property 'length' does not exist on type '{ [key: number]: any[]; }'.
update = () => {
for (let i = 0; i < 7; i++)
{
for (let j = 0; j < this.state.resultData.length; j++) // showing issue in this line
{
}
}
}
解决方案
在界面中,resultData
不是数组。resultData[key]
是数组。所以尝试以下
update = () => {
for (let i = 0; i < 7; i++)
{
for (let j = 0; j < this.state.resultData[i].length; j++)
{
// do something
}
}
}
推荐阅读
- reactjs - 反应无限滚动无法读取所有数据
- wordpress - 在wordpress网站的简单页面中添加下一页和上一页
- expo - React Native Expo:模块注册表中没有字体管理器
- angular - 如何在 RxJs Observable 构造函数中使用异步调用
- flutter - 如何发送列表
数据与方法通道一起颤动? - javascript - Highcharts sankey图中的垂直重新排序节点
- c# - app.Map 和 app.UseEndpoints + endpoints.Map 的区别?
- python - Python 上的指数衰减曲线拟合。错误:优化警告
- node.js - 在 Node.js 中执行图像压缩后,无法从前端获取图像 URL
- php - Pulling random object out of array of objects