我們如何在打字稿中向物件陣列插入值?
我想將 1993 年的值插入到示例物件上的每個 yearRentCurrent 值中。
所以每個yearRentCurrent 值現在都是1993 年。有什么想法嗎?. 我們如何在 typescript 或 angular 中解決這個問題?謝謝。
#data 我想插入
this.transaction.propertyDto.propertyDetailDto.annualRentUsd = 1993;
#sample 物件
this.data.object = [
{
"id": 196,
"name": "Partner Deal ",
"dealType": "Partner Location Submission",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"capitalContribution": null,
"createdOnString": "09/30/2021",
"fileName": null,
"serverFileName": null,
"size": null,
"absoluteUri": null,
"sentTo": null
},
{
"id": 197,
"name": "Buyout Deal Disposition",
"dealType": "Idle Buyout",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"capitalContribution": null,
"createdOnString": "09/30/2021",
"fileName": null,
"serverFileName": null,
"size": null,
"absoluteUri": null,
"sentTo": null
},
{
"id": 199,
"name": "Sublease Deal Disposition",
"dealType": "Idle Sublease",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"capitalContribution": null,
"createdOnString": "09/30/2021",
"fileName": null,
"serverFileName": null,
"size": null,
"absoluteUri": null,
"sentTo": null
},
{
"id": 203,
"name": "Disposition of Location #10532-S",
"dealType": "PM Restructure",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"capitalContribution": null,
"createdOnString": "10/01/2021",
"fileName": null,
"serverFileName": null,
"size": null,
"absoluteUri": null,
"sentTo": null
},
{
"id": 214,
"name": null,
"dealType": "Approval to Close",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"capitalContribution": null,
"createdOnString": "10/04/2021",
"fileName": null,
"serverFileName": null,
"size": null,
"absoluteUri": null,
"sentTo": null
},
{
"id": 215,
"name": "pmpm",
"dealType": "PM Restructure",
"annualRentProposed": null,
"annualRentCurrent": null,
"firmTermRemaining": null,
"firmTermAdded": null,
"maxAvailableTerm": null,
"capitalContribution": null,
"createdOnString": "10/05/2021",
"fileName": null,
"serverFileName": null,
"size": null,
"absoluteUri": null,
"sentTo": null
}
]
uj5u.com熱心網友回復:
由于您只想為陣列中的所有元素設定相同的值,您可以使用map:
this.data.object.map(o => o.annualRentCurrent = 1993);
或forEach:
this.data.object.forEach(o => o.annualRentCurrent = 1993);
它們在這種情況下基本相同,但map會回傳一個新陣列(在這種情況下也會改變原始陣列),而 forEach 只是迭代原始陣列中的每個元素。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/312564.html
標籤:javascript 有角的 angularjs 打字稿
