Skip to content Skip to sidebar Skip to footer

Adding Key Value To An Object In Typescript

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

Solution 1:

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

Post a Comment for "Adding Key Value To An Object In Typescript"