【发布时间】:2022-11-17 01:12:45
【问题描述】:
假设我有以下两个对象
foo = {
a: 10
b: 'hello'
c: 'world'
}
bar = {
a:5
b: null
c: null
d: "This is not in foo"
}
我想要一个操作,它可以执行与以下操作等效的操作,但不必为每个成员指定它。
bar.a ??= foo.a
bar.b ??= foo.b
bar.c ??= foo.c
console.log(bar) // {a:5, b:'hello', c:'world', d:'This is not in foo'
本质上:对于bar的每个成员,如果是nullish则取foo中的值。让所有存在于foo但不存在于bar的成员安息
我该怎么做?我试图以某种方式使用解构搜索解决方案但没有成功......
【问题讨论】:
标签: javascript variable-assignment destructuring