Is there a better approach than repeat all the statments for every cfnumGrantAmount property using javascript?

Joined
Oct 24, 2013
Messages
40
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:
Joined
Sep 4, 2022
Messages
125
Reaction score
16
- as long as you have "code pattern repeating", you can shorten your code by "writing functions".

- you use a JSON data formating, you're working by "associatives Arrays" ( pairs of " keys " and " values " ),
so acting on one single key is easy with associative Arrays.

- to avoid the long long 4 Amount years , you can use an Array in your global Array:
JSON:
.."cftxtIntendedProfession","cfNumGrantAmountYears": {1,2,3,4,5} }
 
Joined
Jul 3, 2022
Messages
93
Reaction score
22
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"
}
],
maxYear = 4, /* what if cfnumGrantAmountY10 exists? */
globalFiscalYear = '2020', /* comes from somewhere else */
globalScholarsL3Data = []; /* target array */

function retVal( o ){
   for(let i = 1; i <= maxYear; i++){
      const n = 'cfnumGrantAmountY' + i,
            Y = i - 1;
      if( !!o[n] ){
         globalScholarsL3Data.push({
                                    "recordid": "0",
                                    "objectid": "0",
                                    "sf_Parent ID": o["recordid"] || "",
                                    "sf_Type ID": "50300",     
                                    "sf_Level Three ID": "0",
                                    "sf_Status ID": "49415",
                                    "cf_1538386": !!globalFiscalYear ? (+globalFiscalYear + Y) : "",
                                    "cf_1538387": o[n]
          });
      }
   }
}

validScholars.forEach( a => retVal( a ) );

console.log( globalScholarsL3Data );
 
Joined
Oct 24, 2013
Messages
40
Reaction score
0
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"
}
],
maxYear = 4, /* what if cfnumGrantAmountY10 exists? */
globalFiscalYear = '2020', /* comes from somewhere else */
globalScholarsL3Data = []; /* target array */

function retVal( o ){
   for(let i = 1; i <= maxYear; i++){
      const n = 'cfnumGrantAmountY' + i,
            Y = i - 1;
      if( !!o[n] ){
         globalScholarsL3Data.push({
                                    "recordid": "0",
                                    "objectid": "0",
                                    "sf_Parent ID": o["recordid"] || "",
                                    "sf_Type ID": "50300",    
                                    "sf_Level Three ID": "0",
                                    "sf_Status ID": "49415",
                                    "cf_1538386": !!globalFiscalYear ? (+globalFiscalYear + Y) : "",
                                    "cf_1538387": o[n]
          });
      }
   }
}

validScholars.forEach( a => retVal( a ) );

console.log( globalScholarsL3Data );
Very smart, working, can you please explain why Y was never defined?
 
Joined
Jul 3, 2022
Messages
93
Reaction score
22
const Y was defined inside the retVal function:

JavaScript:
const n = 'cfnumGrantAmountY' + i,
            Y = i - 1;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,707
Messages
2,569,343
Members
44,635
Latest member
Matt231

Latest Threads

Top