Vin*_*ent 7 javascript destructuring ecmascript-6
我似乎不记得如何编写这个解构模式,你能帮我吗?
假设我有一个这样的对象:
{
id: 'po_495743057439095',
object: 'payout',
amount: 18560,
arrival_date: 1576195200,
automatic: true,
balance_transaction: 'txn_32472309478903284',
created: 1575774863,
currency: 'eur',
description: 'STRIPE PAYOUT',
destination: 'ba_329047329048323',
failure_balance_transaction: null,
failure_code: null,
failure_message: null,
livemode: true,
metadata: {},
method: 'standard',
source_type: 'card',
statement_descriptor: null,
status: 'in_transit',
type: 'bank_account',
}
Run Code Online (Sandbox Code Playgroud)
(这实际上是来自 Stripe API 的一个对象)。
我想将这个对象推送到我的自定义数组中,但只保留 5 个字段。
我可以:
arr.push({
id: myObject.id,
object: myObject.object,
amount: myObject.amount,
arrival_date: myObject.arrival_date,
created: myObject.created
})
Run Code Online (Sandbox Code Playgroud)
但我想知道是否有任何语法可以帮助我以更短的方式实现相同的目标。
基本上我很确定有一种语法可以做到这一点,只需要编写一次“myObject”一词,编写每个键名称一次,并且不创建临时变量,但我不记得如何实现。
我不是在询问编写特定的函数,我不需要帮助。我只是问是否有内置的解构语法可以做到这一点,因为我很确定有,但我找不到它,尽管之前搜索了相当长的时间。
编辑:
我正在寻找的答案就在这里:Elegant way to copy a part of an object
let myObject = {
id: 'po_495743057439095',
object: 'payout',
amount: 18560,
arrival_date: 1576195200,
automatic: true,
balance_transaction: 'txn_32472309478903284',
created: 1575774863,
currency: 'eur',
description: 'STRIPE PAYOUT',
destination: 'ba_329047329048323',
failure_balance_transaction: null,
failure_code: null,
failure_message: null,
livemode: true,
metadata: {},
method: 'standard',
source_type: 'card',
statement_descriptor: null,
status: 'in_transit',
type: 'bank_account',
}
const {id, object, amount} = myObject;
Run Code Online (Sandbox Code Playgroud)
现在myObject.id您将使用id.
| 归档时间: |
|
| 查看次数: |
5179 次 |
| 最近记录: |