在打字稿中向对象数组插入新值
Posted
技术标签:
【中文标题】在打字稿中向对象数组插入新值【英文标题】:inserting new value to an array of object in typescript 【发布时间】:2021-11-26 04:24:22 【问题描述】:我们如何在 typescript 中向对象数组插入值?
我想将 1993 年的值插入到示例对象上的每个年度租金当前值。
所以每个年度租金当前值现在都是 1993 年。有什么想法吗? .我们如何在 typescript 或 angular 中解决这个问题?谢谢。
#我要插入的数据
this.transaction.propertyDto.propertyDetailDto.annualRentUsd = 1993;
#示例对象
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
]
【问题讨论】:
使用RxJs的filter算子怎么样? learnrxjs.io/learn-rxjs/operators/filtering/filter 【参考方案1】:由于您只想为数组中的所有元素设置相同的值,您可以使用map
:
this.data.object.map(o => o.annualRentCurrent = 1993);
或forEach
:
this.data.object.forEach(o => o.annualRentCurrent = 1993);
在这种情况下它们基本上是相同的,但map
将返回一个新数组(在这种情况下也会改变原始数组),而 forEach 只是迭代原始数组中的每个元素。
【讨论】:
您好,先生,如果我想将其设为 2 先生,如 AnnualRentCurrent = 1993 和 AnnualRentProposed = 1994 ,我们如何在地图中做到这一点?this.data.object.map(o => o.annualRentProposed= 1994);
@NikhilVS ,所以每次要修改代码都得重复一遍?
@BrindaAnand 这完全取决于你想要什么。如果您每次都尝试增加值,则可以执行以下操作:let val = 1993; this.data.object.map(o => o.annualRentCurrent = val++);
,然后您将获得 1993-1999 的数字。你也可以调用像this.data.object.map(o => o.annualRentCurrent = getAnnualRent(o);
这样的外部方法,你可以根据对象来计算它。有很多方法可以解决这个问题,具体取决于您想要什么。
不,这两行就可以了。正如@Mordred 所说,有很多方法取决于您的逻辑和便利性。以上是关于在打字稿中向对象数组插入新值的主要内容,如果未能解决你的问题,请参考以下文章