将新值插入打字稿中的对象数组

inserting new value to an array of object in typescript

我们如何向打字稿中的对象数组插入值?

我想将 1993 值插入示例对象的每个 annualRentCurrent 值。

所以每个 annualRentCurrent 值现在都是 1993。伙计们有什么想法吗? .我们如何在打字稿或 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
        }
    ]

因为您只想为数组中的所有元素设置相同的值,您可以只使用 map:

this.data.object.map(o => o.annualRentCurrent = 1993);

forEach:

this.data.object.forEach(o => o.annualRentCurrent = 1993);

在这种情况下它们基本上是相同的,但是 map 将 return 一个新数组(并且在这种情况下也改变原始数组)而 forEach 只是迭代原始数组中的每个元素.