是否可以在初始化时将一个对象的特定键值对添加到另一个对象?
const a = {
android: {
time: null,
diff: null
}
};
const b = {
out:{
x: null,
context:{
state: null,
value: null,
"a.android key-value pairs right here:
time: null,
diff: null"
}
}
};
答案 0 :(得分:7)
您可以合并两个对象,例如:
Object.assign(b.out.context, a.android)
const a = {
android: {
time: null,
diff: null
}
};
let b = {
out:{
x: null,
context:{
state: null,
value: null,
time: 1,
diff: null
}
}
};
Object.assign(b.out.context, a.android);
console.log(b);
UPD
如果要在初始化时执行此操作,则可以使用 ... (扩展)运算符:
const a = {
android: {
time: 1,
diff: 2
}
};
let b = {
out:{
x: null,
context:{
state: null,
value: null,
time: 3,
diff: 4,
...a.android
}
}
};
console.log(b);