首页 > 解决方案 > 在插入键之前检查对象值的角度

问题描述

如果 res.items 对象中的 partnerTermStart 和 partnerTermEnd 不为空,我们如何插入新的键值?

如果 partnerTermStart 和 partnerTermEnd 不为空,则插入新的 key partnerCam where value if based on the computeTermYears result 。

仅当 partnerTermStart 和 partnerTermEnd 不为空时才插入 partnerCam

有没有一种更简洁的方法可以检查每个对象 partnerTermStart 和 partnerTermEnd 不为空,然后插入新密钥插入新密钥 partnerCam。谢谢。

#我当前的代码

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

#插入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);
  }

#样本对象

[
    {
        "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,
   
    },
 
]

标签: angulartypescript

解决方案


除非您不需要对象的副本,否则我更喜欢这样的东西:

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

工作示例

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


推荐阅读