0

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

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

9 Comments

Sir just an additional question , how do we convert in typescript the number to an amount for example the input is 424109 , it should be 424,109 . Thanks.
not in angular template but in code
You might want to look at Intl.NumberFormat: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. This will format numbers according to locale rules.
do we still have to consider the currency?
also what round do we use to round 11.833333 to 11.8
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.