- Joined
- Oct 24, 2013
- Messages
- 47
- Reaction score
- 0
I am repeating the following statements for each Year Amount(e.g cfnumGrantAmountY1,cfnumGrantAmountY2,cfnumGrantAmountY3,cfnumGrantAmountY4) and adding them to the globalScholarsL3Data array if they exist in the validScholars object. Can the following code be improved instead of repeating the following statements for each year:
JavaScript:
const validScholars = [
{
"sfParentID": "8633154",
"cfScholarshipAwardforAcademicYear": "21000",
"sfStatusID": "50298",
"cfScholarPhone": "315-263-7593",
"sfTypeID": "49613",
"cftxtIntendedProfession": "",
"cfnumGrantAmountY1": "100",
"cfnumGrantAmountY2": "101",
"cfnumGrantAmountY3": "102",
"cfnumGrantAmountY4": "103"
},
{
"sfParentID": "8633154",
"cfScholarshipAwardforAcademicYear": "21000",
"sfStatusID": "50298",
"cfScholarPhone": "315-263-7593",
"sfTypeID": "49613",
"cftxtIntendedProfession": "",
"cfnumGrantAmountY1": "1",
"cfnumGrantAmountY2": "2",
"cfnumGrantAmountY3": "",
"cfnumGrantAmountY4": "4"
}
]
validScholars.forEach(item => {
const TempArrayData = [];
if(!!item["cfnumGrantAmountY1"] || !!item["cfnumGrantAmountY2"] || !!item["cfnumGrantAmountY3"] || !!item["cfnumGrantAmountY4"]) {
if(!!item["cfnumGrantAmountY1"]) {
TempArrayData.push({
"recordid":"0",
"objectid":"0",
"sf_Parent ID":item["recordid"],
"sf_Type ID":"50300",
"sf_Level Three ID":"0",
"sf_Status ID":"49415",
"cf_1538386":!!globalFiscalYear ? +globalFiscalYear : "",
"cf_1538387":item["cfnumGrantAmountY1"],
});
}
if(!!item["cfnumGrantAmountY2"]) {
TempArrayData.push({
"recordid":"0",
"objectid":item["recordid"],
"sf_Parent ID":item["recordid"],
"sf_Type ID":"50300",
"sf_Level Three ID":"0",
"sf_Status ID":"49415",
"cf_1538386":!!globalFiscalYear ? +globalFiscalYear + 1 : "",
"cf_1538387":item["cfnumGrantAmountY2"],
});
}
if(!!item["cfnumGrantAmountY3"]) {
TempArrayData.push({
"recordid":"0",
"objectid":item["recordid"],
"sf_Parent ID":item["recordid"],
"sf_Type ID":"50300",
"sf_Level Three ID":"0",
"sf_Status ID":"49415",
"cf_1538386":!!globalFiscalYear ? +globalFiscalYear + 2 : "",
"cf_1538387":item["cfnumGrantAmountY3"],
});
}
if(!!item["cfnumGrantAmountY4"]) {
TempArrayData.push({
"recordid":"0",
"objectid":item["recordid"],
"sf_Parent ID":item["recordid"],
"sf_Type ID":"50300",
"sf_Level Three ID":"0",
"sf_Status ID":"49415",
"cf_1538386":!!globalFiscalYear ? +globalFiscalYear + 3 : "",
"cf_1538387":item["cfnumGrantAmountY4"],
});
}
}
TempArrayData.length && globalScholarsL3Data.push(...TempArrayData);
})
console.log('globalScholarsL3Data', globalScholarsL3Data);
----------------
//Intended globalScholarsL3Data
globalScholarsL3Data = [
{
"recordid": "0",
"objectid": "0",
"sf_Parent ID": "11085716",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2019,
"cf_1538387": "100"
},
{
"recordid": "0",
"objectid": "11085716",
"sf_Parent ID": "11085716",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2020,
"cf_1538387": "101"
},
{
"recordid": "0",
"objectid": "11085716",
"sf_Parent ID": "11085716",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2021,
"cf_1538387": "102"
},
{
"recordid": "0",
"objectid": "11085716",
"sf_Parent ID": "11085716",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2022,
"cf_1538387": "103"
},
{
"recordid": "0",
"objectid": "0",
"sf_Parent ID": "11085717",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2019,
"cf_1538387": "1"
},
{
"recordid": "0",
"objectid": "11085717",
"sf_Parent ID": "11085717",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2020,
"cf_1538387": "2"
},
{
"recordid": "0",
"objectid": "11085717",
"sf_Parent ID": "11085717",
"sf_Type ID": "50300",
"sf_Level Three ID": "0",
"sf_Status ID": "49415",
"cf_1538386": 2022,
"cf_1538387": "4"
}
]
Last edited: