Home > OS >  Angular/typescript mapping an object
Angular/typescript mapping an object

Time:09-23

I have leaseDetails array of objects and I use the setRentSchedule function below to filter the data by Current Term description and then I use the currentTerm object result to pass it to the function name mapRent and get the result object from the mapRent .

Right now I can only do let rentObject = this.mapRent(currentTerm[0]); and it only returns single object like , how do we pass the whole currentTerm object instead of index and mapRent will map each object and return a whole object based on currentTerm . Thanks.

#current result

   {
        "description": "Current Term - Rent Adjustment",
        "monthlyRent": 31470.180000000004,
        "monthsInPeriod": 47.96666666666667,
        "rentInPeriod": 1509519.634
    },

#the result I wanna get , since I am passing currentTerm with 2 object the rentObject should also return 2 objects

{
    "description": "Current Term - Rent Adjustment",
    "monthlyRent": 31470.180000000004,
    "monthsInPeriod": 47.96666666666667,
    "rentInPeriod": 1509519.634
},
{
    "description": "Current Term",
    "monthlyRent": 29971.599999999995,
    "monthsInPeriod": null,
    "rentInPeriod": null
}

#leaseDetails object

leaseDetails: any = [
    {
        "id": 179429,
        "leaseId": 26782,
        "type": "Termination Option",
        "eventStart": null,
        "eventEnd": "2025-04-30T00:00:00",
        "noticeDate": "0001-01-01T00:00:00",
        "monthlyRent": 29971.6,
        "annualRent": 359659.19999999995,
        "available": true,
        "status": "AVAILABLE",
        "description": "Current Term",
        "eventStartString": "",
        "eventEndString": "04/30/2025",
        "noticeDateString": ""
    },
    {
        "id": 179430,
        "leaseId": 26782,
        "type": "Termination Option",
        "eventStart": "2025-05-01T00:00:00",
        "eventEnd": "2029-04-30T00:00:00",
        "noticeDate": "2028-04-30T00:00:00",
        "monthlyRent": 31470.18,
        "annualRent": 377642.16000000003,
        "available": true,
        "status": "AVAILABLE",
        "description": "Current Term - Rent Adjustment",
        "eventStartString": "05/01/2025",
        "eventEndString": "04/30/2029",
        "noticeDateString": "04/30/2028"
    },
    {
        "id": 179423,
        "leaseId": 26782,
        "type": "Termination Option",
        "eventStart": "2029-05-01T00:00:00",
        "eventEnd": "2034-04-30T00:00:00",
        "noticeDate": "2033-04-30T00:00:00",
        "monthlyRent": 33043.69,
        "annualRent": 396524.28,
        "available": true,
        "status": "AVAILABLE",
        "description": "Future Option",
        "eventStartString": "05/01/2029",
        "eventEndString": "04/30/2034",
        "noticeDateString": "04/30/2033"
    },
]

#ts code to filter the leaseDetails based on description

     ngOnChanges(changes: SimpleChanges) { 
        if (changes.leaseDetails && changes.leaseDetails.currentValue){
          this.setRentSchedule();
        }
      }
    
      setRentSchedule(){
    if(this.leaseDetails && this.leaseDetails.leaseCriticalDateDto){
      let indexOption = 1;
      _.sortBy(this.leaseDetails.leaseCriticalDateDto,['eventEnd']).map((r) =>{
        this.rentSchedule.push(
          {
            noticeDate: (r.noticeDateString)? r.noticeDateString : '-',
            description: r.description,
            startDate: r.eventStartString,
            endDate: r.eventEndString,
            annualRent: r.annualRent
          }
        )
      });
      let currentTerm = this.rentSchedule.filter(r => r.description.search("Current Term") !== -1 );
      let rentObject = this.mapRent(currentTerm);
      console.log("rentObject" , rentObject);
    }

  }
  
  mapRent(scheduleData: any):any{
    let startDate = typeof scheduleData.startDate === 'string' ? new Date(scheduleData.startDate):scheduleData.startDate;
    const endDate = typeof scheduleData.endDate === 'string' ? new Date(scheduleData.endDate):scheduleData.endDate;
    return { 
      description: scheduleData.description,
      monthlyRent: scheduleData.annualRent / 12,
      monthsInPeriod: (this.Days360(startDate, endDate) / 360) * 12 ,
      rentInPeriod: (scheduleData.annualRent / 12) * (this.Days360(startDate, endDate) / 360) * 12,
    }
  }

#currentTerm object result based on the code above - all with Current Term is selected from the leaseDetails

[
    {
        "noticeDate": "-",
        "description": "Current Term",
        "startDate": "",
        "endDate": "04/30/2025",
        "annualRent": 359659.19999999995
    },
    {
        "noticeDate": "04/30/2028",
        "description": "Current Term - Rent Adjustment",
        "startDate": "05/01/2025",
        "endDate": "04/30/2029",
        "annualRent": 377642.16000000003
    }
]

CodePudding user response:

You can use map to transform the currentTerm array items into rentObjects like this:

let currentTerm = this.rentSchedule.filter(r => r.description.search("Current Term") !== -1 );
let rentObject = currentTerm.map(ct => this.mapRent(ct));
console.log("rentObject" , rentObject);

Now, the rentObject is an array which contains both items.

To get the total monthsInPeriod for the rentObject array, you can do something like this:

const totalMonthsInPeriod = rentObject
  .map(ro => ro.monthsInPeriod)
  .reduce((currentVal, acc) => acc   currentVal);

For more information, have a look at the Array.reduce method.

  • Related