我有一个源对象,我想用新的目标对象更新其属性值,并返回带有modified属性的源对象。在java脚本中是否有任何预定义的方法我已经这样做了。
var sourceObj = {
context: {
from: {
name: "1",
street: "2"
},
to: {},
weight: 0
}
}
var targetObj = {
name: "3",
street: "4"
}
var result = {
context: {
from: {
name: "1",
street: "2"
},
to: {
name: "3",
street: "4"
},
weight: 0
}
}
答案 0 :(得分:0)
如果你想重复使用这些对象,那就是:
@Bean
public AuthenticationProvider activeDirectoryLdapAuthenticationProvider() {
ActiveDirectoryLdapAuthenticationProvider provider = new ActiveDirectoryLdapAuthenticationProvider(ldapdomain, ldapurl);
provider.setConvertSubErrorCodesToExceptions(true);
provider.setUseAuthenticationRequestCredentials(true);
return provider;
}
如果你想制作浅色副本:
sourceObj.context.to = targetObj;
// (Then don't use `result`)
直播示例:
result = Object.assign({}, sourceObj);
result.context.to = Object.assign({}, targetObj);

var sourceObj = {
context: {
from: {
name: "1",
street: "2"
},
to: {},
weight: 0
}
}
var targetObj = {
name: "3",
street: "4"
}
var result = Object.assign({}, sourceObj);
result.context.to = Object.assign({}, targetObj);
console.log(result);

如果你想制作深拷贝,我会根据上述内容将其作为练习留给读者。 : - )
例如,Object.assign
制作浅物品副本。
还有Stage 3 proposal for rest/spread properties我们可以使用它(它可能会制作ES2018规范,它已经在至少两个JavaScript引擎中实现了):< / p>
.as-console-wrapper {
max-height: 100% !important;
}
实例(需要最先进的浏览器):
result = {...sourceObj};
result.context.to = {...targetObj};
&#13;
var sourceObj = {
context: {
from: {
name: "1",
street: "2"
},
to: {},
weight: 0
}
}
var targetObj = {
name: "3",
street: "4"
}
var result = {...sourceObj};
result.context.to = {...targetObj};
console.log(result);
&#13;
答案 1 :(得分:0)
您可以使用"Next"
浅层复制对象,然后通过直接访问密钥Object.assign()
来更改to
值。
to
&#13;
或者,您可以直接更新var sourceObj = { context: { from: { name: "1", street: "2" }, to:{}, weight: 0 }};
var targetObj = { name: "3", street: "4" };
var result = Object.assign({},sourceObj);
result.context.to = Object.assign({},targetObj);
console.log(result);
的{{1}}密钥。但这将修改您的to
对象。
sourceObj
&#13;