Home > Net >  adding key value to an object in typescript
adding key value to an object in typescript

Time:10-06

How do we insert key and values to an existing array of object based on a condition, can this be solve using map ? in a single line ? . Thanks

If dealType from the object is === PM Restructure insert new key and value and if dealType Partner Location Submission remove annualRentProposed and annualRentCurrent.

Thanks for any idea and help.

cashContribution : 3000, storeCashFlow : 4000,

#data

  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:

We can use Array.map(), using a simple .map() function.

We use a switch statement on dealType to decide how to map each deal.

By default we just leave the deal unmodified.

One could extend the logic used in 'PM Restructure' to add more parameters for other deal types too.

const input = [ { "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 } ]

 // Function that maps each deal according to the dealType        
function mapDeal(deal, parameters) {
    switch (deal.dealType) {

        case 'PM Restructure':
            // Add any extra parameters for PM Restructure
            return { ...deal, ...(parameters['PM Restructure'] || {})};

        case 'Partner Location Submission':
            // Remove annualRentProposed, annualRentCurrent using object destructuring
            const { annualRentProposed, annualRentCurrent, ...rest } = deal;
            return rest;
         
        default:
            // Leave the deal as it is
            return deal;
    }
}
 
function mapDeals(input, parameters) {
    return input.map(deal => mapDeal(deal, parameters));
}
 
const parameters = { 
    'PM Restructure': { 
        cashContribution: 3000,
        storeCashFlow: 4000
    }
}
 
console.log(mapDeals(input, parameters))
    
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related