getting distinct items with sum out of array of objects

get unique values from array java
unique elements in array
unique elements in array c++
find unique elements in array c#
minimum sum of two elements from two arrays
java program to find distinct elements in two arrays
distinct array
find the first unique element in an array of unique and duplicate elements

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 } ]

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);

Group By, Count Distinct, Sum Totals From Objects in JavaScript Array, Having used filter to get the items you want, you can do this in two steps, with a First we filter out the unSelected and get a distinct array of objects by version  How does it work. First the array is remapped in a way that it can be used as an input for a Map.. arr.map(item => [item[key], item]); which means each item of the array will be transformed in another array with 2 elements; the selected key as first element and the entire initial item as second element, this is called an entry (ex. array entries, map entries).

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);

Find sum of non-repeating (distinct) elements in an array , Get Hired · Campus Ambassador Program · Project · Geek of the Month · Campus Geek Given an integer array with repeated elements, the task is to find sum of all distinct elements in array. all elements of array in ascending order and and find one by one distinct elements in array. System.out.println(findSum(arr, n));. }​. ES6 has a native object Set to store unique values. To get an array with unique values you could do now this: var myArray = ['a', 1, 'a', 2, '1']; let unique = [new Set(myArray)]; // unique is ['a', 1, 2, '1'] The constructor of Set takes an iterable object, like Array, and the spread operator transform the set back into an Array.

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.

getting distinct items with sum out of array of objects, getting distinct items with sum out of array of objects. find sum of non-repeating (​distinct) elements in an array minimum unique array sum github minimum  You can emulate the effect of DistinctBy using GroupBy and then just using the first entry in each group. Might be a bit slower that the other implementations though. someList.GroupBy(elem=>elem.Prop2).Select(group=>group.First()); improve this answer. answered Feb 14 '11 at 11:51. 182 silver badges. 237 bronze badges.

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);

Sum of unique elements in array in python, Problem statement: Find out the length of the minimum subarray whose sum is How to get distinct elements from an array by avoiding duplicate elements? getting distinct items with sum out of array of objects. I have really searched and cannot find the right answer. I have an array of objects: 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.

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. :)

jQuery.unique(), Description: Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers. Distinct objects by property value from an array of objects. Sometimes we want to get an array of distinct objects by property value from the original array. For example, we have an array of objects as below. For each object, id and name properties are in 1:1 relationship.

How to Remove Array Duplicates in ES6, 3 ways to filter out duplicates from an array and return only the unique values Set is a new data object introduced in ES6. Retrieve the duplicate values. \$\begingroup\$ Thanks. I disagree with "$.each is designed primarily for iterating the DOM rather than general arrays.". $(selector).each() is used for DOM element, while $.each() is a utility method which can be used instead of Array.prototype.forEach (pre IE9).

Count distinct elements in an array in Python, In a list in Python we may have duplicate elements. When we count the length of the list we get the total length including the duplicate elements. microsoft flow get all items with distinct Assigned to (User and group) filed. Subscribe to RSS Feed. Email to a Friend. Report Inappropriate Content. ‎05-06-2018 11:02 PM. I am creating a flow to get all the item assined to a single user and send an email to that user with all the item detail . I dont want to send separate email for each task .

Set, The Set object lets you store unique values of any type, whether primitive get Set[@@species]: The constructor function that is used to create derived objects. Returns a new Iterator object that contains an array of [ value , value ] for please check out https://github.com/mdn/browser-compat-data and  The Get-Unique cmdlet compares each item in a sorted list to the next item, eliminates duplicates, and returns only one instance of each item. The list must be sorted for the cmdlet to work properly. Get-Unique is case-sensitive.

Comments
  • Can you add an example of what the "array with just two objects" looks like to your question?