在插入键之前检查对象值



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

如果partnerTermStart和partnerTermEnd不为空,则插入新的密钥partnerCam,其中value是基于computeTermYears的结果。

只在partnerTermStart和partnerTermEnd不为空时插入partnerCam

有没有清洁的方法我们可以检查每个对象partnerTermStart partnerTermEnd并不是零,然后插入新键插入新的密钥partnerCam。谢谢。

#my current code

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

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

#样本对象

[
{
"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));

你可以有一个简单的Typescript函数。

for( for item of res.items){
if(item.partnerTermStart != null && item.partnerTermEnd != null) {
// Do you stuff like adding to Array or anything else.
}
}

在你的地图添加检查开始和结束,也在computeTermYears函数而不是null声明computedYearslet 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; 
);

最新更新