How can we add a new key-value pair called partnerCam to the res.items objects when partnerTermStart and partnerTermEnd are not null?
If partnerTermStart and partnerTermEnd have values, then we should insert a new key called partnerCam with a value calculated based on computeTermYears result.
Include partnerCam only if both partnerTermStart and partnerTermEnd are not null.
Is there a more efficient way to validate each object's partnerTermStart and partnerTermEnd before inserting the new key partnerCam? Thank you.
#my current code
const newArr = res.items.map(v => ({...v, partnerCam: this.computeTermYears(new Date(v.partnerTermStart) , v.partnerTermEnd)}))
#function to insert computedDate
computeTermYears(startDate: Date, endDate: Date){
let computedYears = null;
if(startDate && endDate){
const totalDays = AppUtils.Days360(startDate, endDate);
computedYears = totalDays / 360;
}
return this.partnerTerm = computedYears.toFixed(2);
}
#sample object
[
{
"id": 248,
"name": "248-A",
"dealType": "Idle Buyout",
"annualRentProposed": null,
"annualRentCurrent": 349006.08,
"firmTermRemainingCurrent": 17.666666,
"maxAvailableTerm": null,
"cashContribution": null,
"cashFlow": 125535.65376980315,
"description": null,
"wagAnnualCurrent": 349006.08,
"wagFirmTermRemainingCurrent": 17.666666,
"partnerTermStart": "2021-10-28T00:00:00"
"partnerTermEnd": "2021-10-28T00:00:00"
"partnerCam": null,
},
{
"id": 249,
"name": "249-B",
"dealType": "PM Restructure",
"annualRentProposed": null,
"annualRentCurrent": 349006.08,
"firmTermRemainingCurrent": 17.666666,
"maxAvailableTerm": null,
"cashContribution": null,
"cashFlow": 125535.65376980315,
"description": null,
"wagAnnualCurrent": 349006.08,
"wagFirmTermRemainingCurrent": 17.666666,
"partnerTermStart": null,
"partnerTermEnd": null,
},
{
"id": 258,
"name": "251-D (copy)",
"dealType": "Partner Location Submission",
"annualRentProposed": null,
"annualRentCurrent": 349006.08,
"firmTermRemainingCurrent": 17.666666,
"maxAvailableTerm": null,
"cashContribution": null,
"cashFlow": 125535.65376980315,
"description": null,
"wagAnnualCurrent": 349006.08,
"wagFirmTermRemainingCurrent": 17.666666,
"partnerTermStart": "2021-10-28T00:00:00",
"partnerTermEnd": "2021-10-16T00:00:00",
"partnerCam": 2323,
},
]