javascript - 以更清洁的方式将属性传递给此
问题描述
是否有另一种干净的方法来编写此代码,以便将数据数组中的属性直接传递给该对象。
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]});
推荐阅读
- python - 使用上述值简化部分数据框列的有效方法
- reactjs - onError 在 typescript&react 中显示另一张图片
- php - laravel 中带有“AND”和“OR”运算符的多个 where 条件
- python - 通过 FFMPEG API 为传入的 RTSP 流设置 fps
- python - 图像 (png) 作为 MDRectangleFlatIconButton 中的图标
- python - 更改多索引 Pandas
- angular - 在功能模块中使用异步管道
- python - 数据类是否支持指针?
- amazon-web-services - AWS Step 函数 - 选择状态不支持字段参数?
- html - 无法弄清楚为什么“已访问样式”不适用于链接