[英]Revesse object keys in javascript
我注冊后的回復如下:
{
"emailAddress": "test@test.com",
"id": 43,
"uuid": "afb9fd83-b989-40c6-bb81-210e053fe19a",
"createdAt": "2021-07-29T03:13:29.427Z",
"updatedAt": "2021-07-29T03:13:29.427Z"
"user": {
"id": 43,
"uuid": "588cef19-bbaa-4a91-905e-088d9098a1aa",
"firstName": "dadadada2",
"createdAt": "2021-07-29T03:13:29.427Z",
"updatedAt": "2021-07-29T03:13:29.427Z",
},
}
但這與我的 DTO 課程不一致。 我想得到一個結果:
{
"firstName": "dadadada2",
"id": 43,
"uuid": "588cef19-bbaa-4a91-905e-088d9098a1aa",
"createdAt": "2021-07-29T03:13:29.427Z",
"updatedAt": "2021-07-29T03:13:29.427Z",
"authentication": {
"emailAddress": "test@test.com",
"id": 43,
"uuid": "afb9fd83-b989-40c6-bb81-210e053fe19a",
"createdAt": "2021-07-29T03:13:29.427Z",
"updatedAt": "2021-07-29T03:13:29.427Z"
}
}
我試圖在 Typescript 中結合類型和 Partial 方法,但后來我不得不使用any一詞 - 我想避免這種情況。
JS怎么寫函數,誰來背?
1- 為什么不改變你的 DTO
2-也許您可以要求后端開發人員更改數據結構
3-您可以創建一個重構的對象,以避免創建一些復雜的函數來操作對象鍵,而鍵保持靜態。 像 :
const compatWithDTO = {
"firstName": res.user.firstName,
"id": res.id,
"uuid": res.uuid,
"createdAt": res.createdAt,
"updatedAt": res.updatedAt,
"authentication" {
"emailAddress": res.emailAddress,
"id": res.user.id,
"uuid": res.user.uuid,
"createdAt": res.user.createdAt,
"updatedAt": res.user.updatedAt,
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.