Home > Net >  angular checking object values before inserting key
angular checking object values before inserting key

Time:10-09

How do we insert new key value if partnerTermStart and partnerTermEnd are not null from the res.items objects?

if partnerTermStart and partnerTermEnd are not null then insert new key partnerCam where value if based on the computeTermYears result .

insert partnerCam only if partnerTermStart and partnerTermEnd not null

Is there a cleaner way we can check each object partnerTermStart and partnerTermEnd are not null and then insert new key insert new key partnerCam. Thanks.

#my current code

 const newArr = res.items.map(v => ({...v, partnerCam: this.computeTermYears(new Date(v.partnerTermStart) , v.partnerTermEnd)}))

#function to insert computatedDate

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

CodePudding user response:

You can have a simple Typescript function.

for( for item of res.items){
   if(item.partnerTermStart != null && item.partnerTermEnd != null) {

      // Do you stuff like adding to Array or anything else.
   }
}

CodePudding user response:

Unless you don't need copies of your objects, I would prefer something like this:

items.forEach(v => {
  if (v.partnerTermStart && v.partnerTermEnd) {
    v.partnerCam = this.computeTermYears(new Date(v.partnerTermStart), v.partnerTermEnd);
  }
});

Working example

const items = [
    {
        "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,
   
    }, 
];

function computeTermYears(startDate, endDate) {
  let computedYears = null;
  if (startDate && endDate){
    const totalDays = 42;
    computedYears =  totalDays / 360;
  }
  return this.partnerTerm = computedYears.toFixed(2);
}

items.forEach(v => {
  if (v.partnerTermStart && v.partnerTermEnd) {
    v.partnerCam = this.computeTermYears(new Date(v.partnerTermStart), v.partnerTermEnd);
  }
});

console.log(JSON.stringify(items, null, 2));

CodePudding user response:

In your map add check for start and end, also in computeTermYears function instead of null declare computedYears as let computedYears = 0;.

const newArr = res.items.map(v => {
  if(v.partnerTermStart && v.partnerTermEnd){
    return {...v, partnerCam: this.computeTermYears(new Date(v.partnerTermStart) , new Date(v.partnerTermEnd))};
  }
  return v; 
);
  • Related