Home > Net >  Sort jobs by date or day of the week in a javascript object/array
Sort jobs by date or day of the week in a javascript object/array

Time:10-21

I have a project I am working on where I auto-generate a timesheet for a work employee. I get JSON data from a database, then take that JSON data and turn it into a javascript object. Then using Object.entries and forEach(), I iterate over the object data and restructure it into another object with each job punch being the main object property and the value of each "job" being an array of objects with job data in them. One of these array key/value pairs is day_of_week, which is an integer, and another key/value pair is a string month/day date. I would like to sort the jobs in the array by either the day_of_week or the job_date values and update the existing array, or create a new array, it doesn't matter, but I can not figure out how. Any help is appreciated, and code snippet is below of how the object is created. Also, below is the original JSON data, redacted, for where the laborEntries object is initially originated.

{
    "call": ["run_queries"],
    "with": {
        "run_queries": {
            "retrieve_labor_for_payroll_period": [
                ["480192", "6.00", "Technician", "Regular", "2022-09-16", "88628892", "00:00:00", "00:00:00", "REDACTED", "2209161153520", "REDACTED"],
                ["482587", "4.00", "Technician", "Regular", "2022-09-15", "88606868", "00:00:00", "00:00:00", "REDACTED", "2209151349576", "REDACTED"],
                ["480074", "1.00", "OT Technician", "OverTime", "2022-09-16", "88633930", "00:00:00", "00:00:00", "REDACTED", "2209161530274", "REDACTED"]
            ]
        }
    }
} 
 Object.entries(laborEntries).forEach((job => {


                // Populate timesheet with jobs from date-range
                acc  = 1
                timesheet[`job${acc}`] = Object(job[1])
                const jobDate = new Date(job[1][4])
                let isOvertime = false

                timesheet[`job${acc}`]["job_date"] = jobDate.getMonth()   1   '/'   Number(jobDate.getDate()   1)
                timesheet[`job${acc}`]["day_of_week"] = jobDate.getDay()   1
                timesheet[`job${acc}`]["wo_number"] = job[1][0]
                timesheet[`job${acc}`]["job_name"] = job[1][8]
                timesheet[`job${acc}`]["work_type_code"] = 'S, M, or T'
                timesheet[`job${acc}`]["pw_type_code"] = 'pw type code'

                if (job[1][3] === 'OverTime') {
                    isOvertime = true
                }
                timesheet[`job${acc}`]["ot_billed"] = isOvertime

                if (isOvertime) {
                    timesheet[`job${acc}`]["reg_hours"] = 0
                    timesheet[`job${acc}`]["ot_hours"] = Number(`${job[1][1]}`)
                } else {
                    timesheet[`job${acc}`]["reg_hours"] = Number(`${job[1][1]}`)
                    timesheet[`job${acc}`]["ot_hours"] = 0
                }

                timesheet[`job${acc}`]["non_worked_hours"] = `${job[1][1]} -- n/w hours`
                timesheet[`job${acc}`]["non_worked_code"] = 'non-worked code'
                timesheet[`job${acc}`]["is_on_call"] = isOnCall ? '*****ON-CALL WEEK*****' : ''



                // delete non-used obj data
                for (let i = 0; i <= 10; i  ) {
                    delete timesheet[`job${acc}`][i]
                }

CodePudding user response:

I made a try on this. Could you check below code?

var  a = {
    
    "call": ["run_queries"],
    "with": {
        "run_queries": {
            "retrieve_labor_for_payroll_period": [
                ["480192", "6.00", "Technician", "Regular", "2022-09-16", "88628892", "00:00:00", "00:00:00", "REDACTED", "2209161153520", "REDACTED"],
                ["482587", "4.00", "Technician", "Regular", "2022-09-15", "88606868", "00:00:00", "00:00:00", "REDACTED", "2209151349576", "REDACTED"],
                ["480074", "1.00", "OT Technician", "OverTime", "2022-09-16", "88633930", "00:00:00", "00:00:00", "REDACTED", "2209161530274", "REDACTED"]
            ]
        }
    }
};

var laborEntries = a.with.run_queries.retrieve_labor_for_payroll_period;
var acc = 0;
var timesheet = [];
var isOnCall =  true;
Object.entries(laborEntries).forEach((job => {


                // Populate timesheet with jobs from date-range
                acc  = 1
                timesheet[`job${acc}`] = Object(job[1])
                const jobDate = new Date(job[1][4])
                let isOvertime = false

                timesheet[`job${acc}`]["job_date"] = jobDate.getMonth()   1   '/'   Number(jobDate.getDate()   1)
                timesheet[`job${acc}`]["day_of_week"] = jobDate.getDay()   1
                timesheet[`job${acc}`]["wo_number"] = job[1][0]
                timesheet[`job${acc}`]["job_name"] = job[1][8]
                timesheet[`job${acc}`]["work_type_code"] = 'S, M, or T'
                timesheet[`job${acc}`]["pw_type_code"] = 'pw type code'

                if (job[1][3] === 'OverTime') {
                    isOvertime = true
                }
                timesheet[`job${acc}`]["ot_billed"] = isOvertime

                if (isOvertime) {
                    timesheet[`job${acc}`]["reg_hours"] = 0
                    timesheet[`job${acc}`]["ot_hours"] = Number(`${job[1][1]}`)
                } else {
                    timesheet[`job${acc}`]["reg_hours"] = Number(`${job[1][1]}`)
                    timesheet[`job${acc}`]["ot_hours"] = 0
                }

                timesheet[`job${acc}`]["non_worked_hours"] = `${job[1][1]} -- n/w hours`
                timesheet[`job${acc}`]["non_worked_code"] = 'non-worked code'
                timesheet[`job${acc}`]["is_on_call"] = isOnCall ? '*****ON-CALL WEEK*****' : ''



                // delete non-used obj data
                for (let i = 0; i <= 10; i  ) {
                    delete timesheet[`job${acc}`][i]
                }

}));

var newObj = [];
for (var key in timesheet) {
    var obj = timesheet[key];
    obj['identifier'] =  key;
    newObj.push(obj)
}

var newObj = newObj.sort(function(a, b) { return a['day_of_week'] - b['day_of_week']; });

var timesheetSorted = []
for(var i = 0; i < newObj.length; i  ) {
    timesheetSorted[newObj[i]['identifier']] = newObj[i];
    delete timesheetSorted[newObj[i]['identifier']]['identifier']
}
console.log(timesheetSorted)

I get the following sort based on day_of_week

... day_of_week: 5, ...
... day_of_week: 6, ... 
... day_of_week: 6, ...

Please accept the answer if it works. Avid follower @timeonsite.js

  • Related