javascript - JS合并两个对象不起作用
问题描述
我尝试使用以下内容来合并不起作用的对象,如果this.options
将属性放在上面,我需要什么,this.props
否则使用this.props
默认值。
知道如何以更短的方式合并它,我有更多的字段在做同样的事情
this.props!.hostName = this.options.hostName || this.props!.hostName;
this.props!.eventType = this.options.eventType || this.props!.eventType;
this.props!.eventTypeVersion = this.options.eventTypeVersion || this.props!.eventTypeVersion;
this.props!.uri = this.options.uri || this.props!.uri;
我已经尝试了以下不起作用
this.props = Object.assign(this.props, this.options);
还有这个
this.props = Object.assign(this.options, this.props);
解决方案
使用扩展运算符:
{...props, ...options}
或者
for (const [k, v] of Object.entries(props)) {
props[k] = options[k] || v
};
推荐阅读
- symfony - Symfony 4 SwiftMailer 不发送电子邮件
- android - 如何使用 apache OpenNLP 解决 IllegalStateException 和 ParserConfigurationException 以在 android studio 中查找位置
- android - 当键盘出现 API 26(Samsung S8)时,Android Popupwindow 不会在正确的位置更新
- html - 使用较少的不透明度增加的背景颜色
- angular - 角度表单值将父表单数组页面设置为子表单数组页面
- java - 布冯针模拟
- mysql - 来自同一基础表的两个 GROUP BY 子查询的列总和
- oracle - 员工供应商名称海量更新-融合采购云
- php - PHP中的MongoDB更新:在多个ID上更新一个变量(表示为变量)
- csv - 使用 VBScript 在 CSV 中查找特定类型的字符串