Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Javascript中从foreach访问类变量_Javascript_React Native - Fatal编程技术网

如何在Javascript中从foreach访问类变量

如何在Javascript中从foreach访问类变量,javascript,react-native,Javascript,React Native,我想将数据推送到this.organizationIDList。我推的时候出错了。如何将数据推送到该变量 class NewsScreen extends React.Component { render() { this.organizationIDList = []; this.organizations.forEach(function (organization) { this.organizationIDLis

我想将数据推送到
this.organizationIDList
。我推的时候出错了。如何将数据推送到该变量

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

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

或者将
this
传递给接受
thisArg

            this.organizations.forEach(function (organization) {
                this.organizationIDList.push(organization.id);
            }, this);
上述代码也可以写成:

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

或者将
this
传递给接受
thisArg

            this.organizations.forEach(function (organization) {
                this.organizationIDList.push(organization.id);
            }, this);
上述代码也可以写成:

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

使用箭头函数,如下所示

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

使用箭头函数,如下所示

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