对象解构的正确方法
Correct way of object destructuring
我有一个场景,我从承诺中收到一个对象,需要将这个对象的一些键添加到另一个对象。例如:
// Received from promise
object_1 = {
name: 'SH'
};
// Want to add object_1.name to object_2
object_2 = {
id: 1234
};
通常我可以像下面那样做,但我想用对象解构来做
object_2.name = object_1.name;
拥有:
object_2 = {
id: 1234,
name: 'SH'
};
您可以使用 destructuring assignment to a target object/property with a object property assignment pattern [YDKJS: ES6 & Beyond].
var object_1 = { name: 'SH' },
object_2 = { id: 1234 };
({ name: object_2.name } = object_1);
console.log(object_2);
您可以像这样使用对象析构来实现预期的输出:
// 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
}
我有一个场景,我从承诺中收到一个对象,需要将这个对象的一些键添加到另一个对象。例如:
// Received from promise
object_1 = {
name: 'SH'
};
// Want to add object_1.name to object_2
object_2 = {
id: 1234
};
通常我可以像下面那样做,但我想用对象解构来做
object_2.name = object_1.name;
拥有:
object_2 = {
id: 1234,
name: 'SH'
};
您可以使用 destructuring assignment to a target object/property with a object property assignment pattern [YDKJS: ES6 & Beyond].
var object_1 = { name: 'SH' },
object_2 = { id: 1234 };
({ name: object_2.name } = object_1);
console.log(object_2);
您可以像这样使用对象析构来实现预期的输出:
// 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
}