以更简洁的方式传递属性给this



是否有另一种干净的方法来编写这段代码,以便将数据数组中的属性直接传递给该对象。

this.Email = data[0].Email;
this.RealName = data[0].RealName;
this.JobTitle = data[0].JobTitle;
this.UserDID = data[0].UserDID;
this.CreatedDateTime = data[0].CreatedDateTime;
this.ApplicationCount = data[0].ApplicationCount;
this.CountApply = data[0].CountApply;
this.CountResume = data[0].CountResume;
this.LastEmailAction = data[0].LastEmailAction;
this.CountEmailActions = data[0].CountEmailActions;
this.LastResume = data[0].LastResume;
this.LastApply = data[0].LastApply;

如果你想把data[0]的所有属性赋值给this所指向的对象,你可以使用Object.assign(),它会将源对象的所有可枚举属性复制到目标对象:

Object.assign(this, data[0]);

如果您只想选择属性,那么您可以创建这些属性的列表并遍历它们:

['Email', 'RealName', ...].forEach(prop => {this[prop] = data[0][prop]});

相关内容

最新更新