use*_*107 1 javascript arrays state this reactjs
我有一个包含字符串的数组,我想将这些项目作为单个字符串返回,但它们之间有空格.
listProperties() {
    this.properties = "";
    this.state.withoutSuit.forEach(function (i, j) { 
      this.properties += i;
      this.properties += " ";
    });
    console.log(this.properties);
    return this.properties;
  }  
所以在构造函数中我将this.state.withoutSuit作为我的数组,并将this.properties作为我将存储其间隔开的字符串版本的地方.
在函数中,我首先将this.properties设置为空字符串,然后我想用withoutSuit数组项填充它.
但是当我进入forEach循环时,this.properties是未定义的.我认为这是因为"this"现在指的不是构造函数,而是指this.state.withoutSuit: - 是吗?
如果是这样,我如何从循环中引用属性变量?
谢谢!
你可以使用箭头功能
listProperties() {
    this.properties = "";
    this.state.withoutSuit.forEach((i, j)=> { 
      this.properties += i;
      this.properties += " ";
    });
    console.log(this.properties);
    return this.properties;
} 
| 归档时间: | 
 | 
| 查看次数: | 123 次 | 
| 最近记录: |