angular - 在插入键之前检查对象值的角度
问题描述
如果 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,
},
]
解决方案
除非您不需要对象的副本,否则我更喜欢这样的东西:
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));
推荐阅读
- python - 如何限制请求执行时间?
- c++ - 使用 boost asio 发送带外数据
- php - 如何在类别树Codeigniter中显示当前类别
- c# - 我在 asp.net C# MVC 中使用 dapper 映射数据时出错
- java - 读取内容URI时Android FileNotFound异常
- python - 烧瓶 send_file() 发送空文本文档
- android - TabLayout 和 AppBarLayout 有什么区别?
- spring-boot - eventBus().consumer() 永远不会被调用
- java - 如何在spring boot中使用cron作业根据指定条件安排作业
- python - 打印函数返回不同的值