2017-11-14 1 views
0

데이터를 this.organizationIDList으로 푸시하고 싶습니다. 밀어 넣는 동안 정의되지 않은 오류가 발생합니다. 해당 변수에 데이터를 푸시하는 방법은 무엇입니까?자바 스크립트에서 foreach 클래스 변수에 액세스하는 방법

class NewsScreen extends React.Component { 
    render() { 
     this.organizationIDList = []; 

     this.organizations.forEach(function (organization) { 
      this.organizationIDList.push(organization.id); 
     }); 
    } 
} 

답변

1

사용 arrow function

  this.organizations.forEach((organization) => { 
       this.organizationIDList.push(organization.id); 
      }); 

아니면 thisArg

  this.organizations.forEach(function (organization) { 
       this.organizationIDList.push(organization.id); 
      }, this); 

받아 위의 코드도 같이 쓸 수있는 forEachthis를 전달합니다

this.organizationIdList = this.organizations.map(organization =>organization.id); 
1

아래와 같이 화살표 기능을 사용하십시오.

this.organizations.forEach((organization) => { 
    this.organizationIDList.push(organization.id); 
}); 
관련 문제