1

with underscore, I have successfully implemented the logic to group objects by multiple properties and sum the groups, but I haven't found the way to do it with lodash.

Here is the code:

let test = _.chain(response.counterValues)
  .flatten()
  .groupBy(response.counterValues, (item) => {
      return item.duration + '#' + item.timeFrom + '#' + item.timeTo + '#' + item.objectClass;
  })
  .mapValues((value, key) => {
    let sum = _.reduce(value, (memo, val) => {return memo + val.count}, 0);
    let keyItems = key.split('#');
    let duration = keyItems[0];
    let timeFrom = keyItems[1];
    let timeTo = keyItems[2];
    let objectClass = keyItems[3];
    return {duration: duration, timeFrom: timeFrom, timeTo: timeTo, objectClass: objectClass, count: sum};
  }).value();
console.log(test);

and these are example values:

0: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}
1: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 3}
2: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}
3: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}
4: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 3}
5: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 2}
6: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}
7: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 2}
8: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 4}
9: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}
10: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 2}
11: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 2}
12: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 2}
13: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 3}
14: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 2}
15: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}
16: {duration: "h", timeFrom: "1547323200000", timeTo: "1547326800000", objectClass: "car", count: 1}

For these objects, I would like to get the following result:

{duration: "h", timeFrom: 1547323200000, timeTo: 1547326800000, objectClass: "car", count: 32}

Does anyone know how to modify the code above in order to get the desired result?

2
  • Without actual code I was wondering why you want to stick to lodash in the first place. I'd most likely use Array.reduce and dynamically build the resulting object. To add more details: Most likely I'd use Array.reduce to return a Record<GroupType, ResultType> and either add a new element to the Record or updating the existing element while looping through the initial array. Commented Jan 12, 2019 at 23:55
  • Does the output must have timeFrom the min time from and timeTo the max or it's irrelevant since each item has them equal? Commented Jan 12, 2019 at 23:59

1 Answer 1

1
.groupBy(response.counterValues, (item) => { ...

should be

.groupBy((item) => { ...

because you're using _.chain()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.