0

What I did

I call a function in objects' value.

And this function would be called in every object as much as users add an absent data.

What I want to do

Hence I recognize that I could have thousands of hundred of new object, I would rather minimize/eliminate duplication than write the function every time.

Explain what the code is for

The function's name I would like to auto-set is datesBetween().

datesBetween() can choose the mode, and it has parameters.

The mode refers the value from the key reason which is in same object. arr[0].absent[0].datesBetween's value should be datesBetween('leave')('2020-1-1','2020-1-4').

The parameters does same. First parameter should refer from the object key start which is in same object, second parameter should refer from the object key end.

Some idea

  1. I guess this could use class but I am afraid to mix up with functions. if class would help, please do tell.
  2. I write this thou, I am uncertain about this. Is using this could be a solution in this code?
    var arr = [
      {
        name : "A", 
        absent :[ 
          {
            reason : "leave",
            start : '2020-1-1',
            end : '2020-1-4',
            datesBetween : datesBetween('leave')(this.start, this.end)
          }, {
            reason : "sleeOver",
            start : '2020-1-25',
            end : '2020-1-26',
            datesBetween : datesBetween('sleeOver')(this.start, this.end)
          }    
        ]
      }, {
        name : "B", 
        absent :[
          {
            reason : "weekendExcursion",
            start : '2020-1-18',
            end : '2020-1-19',
            datesBetween : datesBetween('weekendExcursion')(this.start, this.end)
          }
        ]
      }  
    ]
    
    function autoAbsentSetter(){
      //do I need class to use inheritance/property things?
      
    }
    
    function addAbsent(mode){
      var funcs = {
        'leave' : function absent_leave(name, absentName, absentStart, absentEnd){ //all kinds of leave are included. Detail further.
        //some codes
        },
        'sleepOver' : function absent_sleepOver(name, absentName, absentStart, absentEnd){
        //some codes
        },
        'weekdayExcursion' : function absent_weekdayExcursion(name, absentName, absentStart, absentEnd){
        //some codes
        },
        'weekendExcursion' : function absent_weekendExcursion(name, absentName, absentStart, absentEnd){
        //some codes
        }
      }
      return funcs[mode];
    }
Just Doo
  • 91
  • 8

1 Answers1

0

You don't need to put those functions inside the array literal (and yes, you cannot access the other properties there anyway). Just write

var arr = [
  {
    name: "A", 
    absent: [ 
      {
        reason : "leave",
        start : '2020-1-1',
        end : '2020-1-4',
      }, {
        reason : "sleepOver",
        start : '2020-1-25',
        end : '2020-1-26',
      }    
    ]
  }, {
    name: "B", 
    absent: [
      {
        reason : "weekendExcursion",
        start : '2020-1-18',
        end : '2020-1-19',
      }
    ]
  }  
];

for (const value of arr) {
  for (const absence of value.absent) {
    absence.datesBetween = datesBetween(absence.reason)(absence.start, absence.end);
  }
}

You can do whatever you want in that loop processing your data.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375