Home > Back-end >  inserting new value to an array of object in typescript
inserting new value to an array of object in typescript

Time:10-06

How do we insert a value to an array of object in typescript ?

I wanna insert 1993 value to each annualRentCurrent value on the sample object.

So each annualRentCurrent value would be now 1993. Any idea guys? . How do we address this in typescript or in angular ? Thanks.

#data I wanna insert

this.transaction.propertyDto.propertyDetailDto.annualRentUsd = 1993;

#sample object

   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
        }
    ]

CodePudding user response:

Since you're just wanting to set the same value across all elements in the array, you can just use map:

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

or forEach:

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

They're basically identical in this scenario, but map will return a new array (and in this case also altering the original) while forEach just iterates over each element in the original array.

  • Related