首页 > 解决方案 > 类型 '{ [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 
          {

          }  
      }  
    } 

标签: javascriptangularreactjs

解决方案


在界面中,resultData不是数组。resultData[key]是数组。所以尝试以下

update = () => { 
  for (let i = 0; i < 7; i++) 
  {   
    for (let j = 0; j < this.state.resultData[i].length; j++)
    {
      // do something
    }  
  }  
}

推荐阅读