如何解决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);
解决方法
使用点差运算符:
map( .Body[].webServerName |
select( ( . != null and endswith(":443") ) and
( test("server-internal") | not ) ) ) |
unique[]
OR
{...props,...options}
,
您的第一次尝试Object.assign(this.props,this.options)
应该有效。如果不是这样,则可能意味着this.options
具有一些您想忽略的虚假属性。您可以尝试将其过滤掉:
const truthyOptions = Object.entries(this.options)
.filter(([k,v]) => v)
.map(([k,v]) => ({[k]: v}))
this.props = Object.assign(this.props,...truthyOptions)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。