[英]Correct way of object destructuring
I have a scenario, where I receive an obj from a promise, and need to add some keys of this object to another object.我有一个场景,我从承诺中收到一个 obj,并且需要将此对象的一些键添加到另一个对象。 For example:
例如:
// Received from promise
object_1 = {
name: 'SH'
};
// Want to add object_1.name to object_2
object_2 = {
id: 1234
};
Normally I could do like following, but I want to do it with object destructuring通常我可以这样做,但我想用对象解构来做
object_2.name = object_1.name;
to have:具有:
object_2 = {
id: 1234,
name: 'SH'
};
You could use a destructuring assignment to a target object/property with a object property assignment pattern [YDKJS: ES6 & Beyond] .您可以使用对象属性分配模式 [YDKJS: ES6 & Beyond]对目标对象/属性进行解构分配。
var object_1 = { name: 'SH' }, object_2 = { id: 1234 }; ({ name: object_2.name } = object_1); console.log(object_2);
You can achieve the expected output by using object destructing like this:您可以像这样使用对象析构来实现预期的输出:
// Received from promise
object_1 = {
name: 'SH'
};
// Want to add object_1.name to object_2
object_2 = {
id: 1234
};
object_2 = {
...object_2,
...object_1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.