我想要实现的基本上是这样的:
R.mergeDeepRight(
{ age: 40, contact: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcbebdbd9cb9a4bdb1acb0b9f2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>' }},
{ name: 'fred', age: 10, contact: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cfa2a0a08faab7aea2bfa3aae1aca0a2" rel="noreferrer noopener nofollow">[email protected]</a>' }}
);
但结果对象中没有 { name: 'fred' }
。
仅应应用第一个对象中的键。
最佳答案
我会结合 mergeDeepRight
制作一个可重用的函数, pick
,和 keys
,像这样:
const funkyMerge = (o1, o2) =>
mergeDeepRight(o1, pick(keys(o1), o2))
console.log(funkyMerge(
{ age: 40, contact: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b0b3b392b7aab3bfa2beb7fcb1bdbf" rel="noreferrer noopener nofollow">[email protected]</a>' }},
{ name: 'fred', age: 10, contact: { email: '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9af7f5f5daffe2fbf7eaf6ffb4f9f5f7" rel="noreferrer noopener nofollow">[email protected]</a>' }}
))
<script src="https://bundle.run/<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7705161a131637475945415946" rel="noreferrer noopener nofollow">[email protected]</a>"></script><script>
const {mergeDeepRight, pick, keys} = ramda </script>
关于javascript - 同步两个对象并应用新值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55665588/