0

I have really searched and cannot find the right answer. I have an array of objects:

[ { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '505',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '404',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '303',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '203',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 950.4000000000001 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '202',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '500',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '400',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '300',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '201',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 864 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '200',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 } ]

All I want is a new array of objects showing currentMonth, employeeNumber, caLastName, caFirstName, rate_per_hour, clientType, the sum of totalHours and the sum of wages for each employee. So in this example, an array with just two objects.

I have tried reduce and filter but I cannot fathom exactly where I am going wrong. I would rather not do a loop as I suspect that would be a lot less efficient.

The new array would look a bit like:

[ { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 168,
    wages: 7286.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 168,
    wages: 6624 } ]
Wayne
  • 283
  • 1
  • 3
  • 21

6 Answers6

4

You can make use of reduce and aggregate the data like

const data = [ { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '505',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '404',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '303',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '203',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 950.4000000000001 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '202',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '500',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '400',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '300',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '201',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 864 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '200',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 } ]
    
const res = data.reduce((acc, obj) => {
     const index = acc.findIndex(item => item.employeeNumber === obj.employeeNumber);
     if(index > -1) {
         console.log(acc[index])
         acc[index] = {...acc[index], totalHours: acc[index].totalHours + obj.totalHours, wages: acc[index].wages + obj.wages};
     } else {
         acc = acc.concat([obj]);
     }
     return acc;
}, [])

console.log(res);
Shubham Khatri
  • 270,417
  • 55
  • 406
  • 400
1

I came up with a simple solution

const result = arr.reduce(function(acc, item) {
  let found = acc.find(processedItem => processedItem.employeeNumber === item.employeeNumber);
  if (found) {
    found.totalHours += item.totalHours;
    found.wages += item.wages;
  } else acc.push(item);
  return acc;

}, []);

console.log(result);
Olena Horal
  • 1,166
  • 3
  • 11
  • 26
1

A similar question is asked and answered here: sum values in object if multiple keys are the same JS

A rough explanation of the answer:

1) You use a loop to go through each item

2) Generate a 'key' out of all the properties that you want to group by (currentMonth, employeeNumber, caLastName, caFirstName, rate_per_hour, clientType)

3) Sum the value(s) you want to sum.

The important part here is that you save the "key" and value into an object outside the loop, which you can reference back to if you find another object in your source array with the same "key". You'll need to split the keys on the "|" character to get the individual properties back.

The example I link uses reduce, which you can modify for your purpose, but you can also apply the same logic in a for loop.

For smaller datasets, for tends to outperform reduce, and with larger sets reduce makes performance gains, but almost always, the design of the algorithm has an order of magnitude larger impact on performance than the looping method you use. So I'd recommend using the convention you're most comfortable with and which you feel you can write the cleanest and most efficient algorithm.

broccoli_rob
  • 520
  • 5
  • 16
1

You could also use:

const data = [ { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '505',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '404',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '303',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '203',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 950.4000000000001 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '202',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '500',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '400',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '300',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '201',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 864 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '200',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 } ];

const employees = data.reduce((result, employee) => {
  const key = employee.caLastName + " " + employee.caFirstName;

  if (result[key]) {
    result[key].totalHours += employee.totalHours;
    result[key].wages += employee.wages;
  } else {
    result[key] = Object.assign({}, employee);
  }
  return result;
}, {});

console.log(employees);
ᴘᴀɴᴀʏɪᴏᴛɪs
  • 7,169
  • 9
  • 50
  • 81
1

Remember that with reduce you probably want to specify an initial state of the accumulator (an empty object sounds appropriate in this instance) and that your reducing function returns that accumulator so that it can be used in the next iteration.

Here's the code that I whipped up in the console that should get you going in the right direction. I saved your example object into a variable descriptively named x:

x.reduce(function(acc, obj){
  // Assuming that we don't know that every entry in the array has the same currentMonth value, let's group our accumulations by month
  // This line will create a new empty object for the current month if we haven't got an entry for the month yet
  // For simple "Initialize if it's not there yet" statements, I like to use the following shorthand

  acc[obj.currentMonth] = acc[obj.currentMonth] || {};
  var monthObject = acc[obj.currentMonth];

  // Assuming that we may have array entries with different employees and that employee number is a unique way to identify them, let's make objects for each employee
  // If we don't have an entry for this employee yet in our accumulator, initialize one and save the global values
  // We will initialize hours and wages to 0 so that we have an integer defined for that value for the next step.

  monthObject[obj.employeeNumber] = monthObject[obj.employeeNumber] || { surname: obj.caLastName, firstname: obj.caFirstName, totalHours: 0, totalWages: 0 };
  var employeeEntry = monthObject[obj.employeeNumber];

  // Now, let's add the current object's values to the appropriate number fields
  employeeEntry.totalHours += obj.totalHours;
  employeeEntry.totalWages += obj.wages;

  return acc;
}, {})

Hopefully the above code and comments give you enough of a head start that you can build out your reduce functions without any trouble in the future. :)

Dave B
  • 3,038
  • 1
  • 13
  • 18
1

You can iterate the array using reduce function and inside the reduce function you can check if item is already exist or not. But the time complexity will be n^2 for this.

let array = [ { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '505',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '404',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '303',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 633.6 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '203',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 950.4000000000001 },
  { currentMonth: 'September-2018',
    employeeNumber: '130',
    caLastName: 'Bartoletti',
    caFirstName: 'Elias',
    payrollCode: '202',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2534.4 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '500',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '400',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '300',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 576 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '201',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 24,
    wages: 864 },
  { currentMonth: 'September-2018',
    employeeNumber: '196',
    caLastName: 'Barrows',
    caFirstName: 'Felicia',
    payrollCode: '200',
    rate_per_hour: 25,
    clientType: 'Single',
    totalHours: 48,
    wages: 2304 } ];

let myArray = array.reduce((arr, value)=>{
    let index = arr.findIndex( item => item.employeeNumber == value.employeeNumber  );
    if( index >= 0 ){
       arr[index].wages += value.wages;
       arr[index].totalHours += value.totalHours;
    } else {
        arr.push(value);
    }
    console.log(index);

    return arr;
}, []);
console.log(myArray);

You can also achieve the above result using object-key mapping(dict) with time complexity n. Here is the following code:

let myArray2 = array.reduce((obj, value)=>{
    if( obj[value.employeeNumber] ){
       obj[value.employeeNumber].totalHours += value.totalHours;
       obj[value.employeeNumber].wages += value.wages
    } else {
       obj[value.employeeNumber] = Object.assign({}, value );
    }
     return obj;
}, {});
let finalArrayValues = Object.values(myArray2)
console.log(finalArrayValues);
Pankaj Jatav
  • 2,158
  • 2
  • 14
  • 22