Shorthand 匹配 属性 名称的对象初始值设定项语法
Shorthand object initializer syntax for matching property name
有时我发现自己需要使用 属性 来初始化一个对象,该 属性 与另一个对象的 属性 相匹配。当 属性 名称相同时,我希望能够使用 shorthand 语法。
(出于本问题示例的目的,我将只保留 tag: 1
属性 的附加属性,我将重用 message
在随后的示例中作为信息的 input/source。我还指出 message
的额外 unwanted
属性 因为我正在挑选属性并且不打算只需使用 Object.assign
将 message
的所有属性分配给 result
.)
const message = {
person: {
name: 'John'
},
unwanted: 'x'
};
let result = { person: message.person, tag: 1 }; // Looking for shorthand for this
为了构建上面的 result
对象,我需要输入 person
两次。我在想一定有一个 shorthand 的方法来做到这一点。我期望它起作用的原因是存在像 ES2015 Shorthand property names and Destructuring assignment 这样的功能。例如:
const { person } = message; // destructing assignment
let result = { person, tag: 1 }; // shorthand property name for `person`
这将创建一个名为 person
的额外变量,其值为 message.person
,结果将有一个名为 person
的 属性 具有所需的值.但是,如果没有变量存在,那么在这种情况下我不知道如何使用 shorthand 。而且我还没有找到将这两个语法特征一起应用的方法。
这是我对语法的第一个直观猜测:
// hoping destructuring assignment is allowed in object literal
let result = { {person} = message, tag: 1 }; // it is not legal :(
我的第二个猜测是:
// hoping that a property name would magically be inferred from `person`
let result = { message.person, tag: 1 }; // it is not legal :(
作为最后的手段,我尝试了 Object.assign
,但它复制了 unwanted
属性,并且不只是 message
的 person
属性 .
let result = Object.assign({ tag: 1 }, message); // Assigns unwanted properties :(
到目前为止我最好的是{ person: message.person, tag: 1 }
是否有 shorthand 初始化语法来实现这个?
The best I have so far is { person: message.person, tag: 1 }
.
Is there shorthand initializer syntax to achieve this?
不,这仍然是他们要走的路。
hoping that a property name would magically be inferred from person
let result = { message.person, tag: 1 };
ECMAScript Shorthand Property Assignment Improvements proposal 正好可以做到这一点。不幸的是,它仍处于第 0 阶段 :-/
假设您使用的是 ES6:
const message = {
person: {
name: 'John'
},
unwanted: 'x'
};
let { unwanted, ...result } = { ...message, tag: 1 };
console.log(result);
如果您想将 unwanted
变量重命名为某个名称,则 dit
let { unwanted: dummy, ...result } = { ...message, tag: 1 };
这些有一些问题,例如
- 这会创建一个额外的变量
unwanted
(如果您使用第二种方法,则为 dummy
)
- 如果你有多个不需要的属性,你必须把它们都写在解构中
所以在我看来,你最好按照你在问题中描述的方式去做。
有时我发现自己需要使用 属性 来初始化一个对象,该 属性 与另一个对象的 属性 相匹配。当 属性 名称相同时,我希望能够使用 shorthand 语法。
(出于本问题示例的目的,我将只保留 tag: 1
属性 的附加属性,我将重用 message
在随后的示例中作为信息的 input/source。我还指出 message
的额外 unwanted
属性 因为我正在挑选属性并且不打算只需使用 Object.assign
将 message
的所有属性分配给 result
.)
const message = {
person: {
name: 'John'
},
unwanted: 'x'
};
let result = { person: message.person, tag: 1 }; // Looking for shorthand for this
为了构建上面的 result
对象,我需要输入 person
两次。我在想一定有一个 shorthand 的方法来做到这一点。我期望它起作用的原因是存在像 ES2015 Shorthand property names and Destructuring assignment 这样的功能。例如:
const { person } = message; // destructing assignment
let result = { person, tag: 1 }; // shorthand property name for `person`
这将创建一个名为 person
的额外变量,其值为 message.person
,结果将有一个名为 person
的 属性 具有所需的值.但是,如果没有变量存在,那么在这种情况下我不知道如何使用 shorthand 。而且我还没有找到将这两个语法特征一起应用的方法。
这是我对语法的第一个直观猜测:
// hoping destructuring assignment is allowed in object literal
let result = { {person} = message, tag: 1 }; // it is not legal :(
我的第二个猜测是:
// hoping that a property name would magically be inferred from `person`
let result = { message.person, tag: 1 }; // it is not legal :(
作为最后的手段,我尝试了 Object.assign
,但它复制了 unwanted
属性,并且不只是 message
的 person
属性 .
let result = Object.assign({ tag: 1 }, message); // Assigns unwanted properties :(
到目前为止我最好的是{ person: message.person, tag: 1 }
是否有 shorthand 初始化语法来实现这个?
The best I have so far is
{ person: message.person, tag: 1 }
.Is there shorthand initializer syntax to achieve this?
不,这仍然是他们要走的路。
hoping that a property name would magically be inferred from
person
let result = { message.person, tag: 1 };
ECMAScript Shorthand Property Assignment Improvements proposal 正好可以做到这一点。不幸的是,它仍处于第 0 阶段 :-/
假设您使用的是 ES6:
const message = {
person: {
name: 'John'
},
unwanted: 'x'
};
let { unwanted, ...result } = { ...message, tag: 1 };
console.log(result);
如果您想将 unwanted
变量重命名为某个名称,则 dit
let { unwanted: dummy, ...result } = { ...message, tag: 1 };
这些有一些问题,例如
- 这会创建一个额外的变量
unwanted
(如果您使用第二种方法,则为dummy
) - 如果你有多个不需要的属性,你必须把它们都写在解构中
所以在我看来,你最好按照你在问题中描述的方式去做。