简体   繁体   English

lodash groupby 和 sum 对象属性

[英]lodash groupby and sum object properties

I have an array of Objects that I need to reduce down based on a property and sum the other property.我有一个对象数组,我需要根据一个属性减少它们并对另一个属性求和。 This solution is pretty much what I'm after , but I need to avoid using _.chain as am importing only specific modules. 这个解决方案几乎是我所追求的,但我需要避免使用_.chain作为只导入特定模块。

I've tried using flow, but am not getting any values:我试过使用流,但没有得到任何值:

const sumBy = require('lodash/fp/sumBy');
const flow = require('lodash/fp/flow');
const groupBy = require('lodash/fp/groupBy');
const map = require('lodash/fp/map');

const list = [
    { property: 'foo', value: 1 },
    { property: 'foo', value: 3 },
    { property: 'bar', value: 2 },
    { property: 'bar', value: 3 },
    { property: 'chu', value: 8 },
];

const foo = flow(
  groupBy('property'),
  map((objs, key) => {
    return {
      property: key,
      value: sumBy(objs, 'value')
    }
  })
)(list)

This returns这返回

[
    { property: undefined, value: 0 },
    { property: undefined, value: 0 },
    { property: undefined, value: 0 }
]

Ideally, I'd like to do this in vanilla es6, I've had a go at it, but not sure how to do the sum.理想情况下,我想在 vanilla es6 中执行此操作,我已经尝试过了,但不确定如何计算总和。

const bar = list.reduce((o, v) => {
    return {...o, [v.property]: v.value}
}, {})

How about the following ES6 only solution:以下仅 ES6 的解决方案如何:

 const list = [ { property: 'foo', value: 1 }, { property: 'foo', value: 3 }, { property: 'bar', value: 2 }, { property: 'bar', value: 3 }, { property: 'chu', value: 8 }, ]; const result = list.reduce((obj, el) => { obj[el.property] = (obj[el.property] || 0) + el.value; return obj; }, {}); console.log(result);

You could take a Map .你可以拿一张Map

 const list = [{ property: 'foo', value: 1 }, { property: 'foo', value: 3 }, { property: 'bar', value: 2 }, { property: 'bar', value: 3 }, { property: 'chu', value: 8 }], result = Array.from( list.reduce((m, { property, value }) => m.set(property, (m.get(property) || 0) + value), new Map), (([property, value]) => ({ property, value })) ); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

With lodash用 lodash

 const list = [{ property: 'foo', value: 1 }, { property: 'foo', value: 3 }, { property: 'bar', value: 2 }, { property: 'bar', value: 3 }, { property: 'chu', value: 8 }], result = _(list) .groupBy('property') .map((array, property) => ({ property, value: _.sumBy(array, 'value')})) ; console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>

This could be thought of as a less efficient but nicer-to-read variant of the answer from Ruben Helsloot.这可以被认为是 Ruben Helsloot 答案的一种效率较低但更易于阅读的变体。

 const sumValues = list => list .reduce ((obj, {property, value}) => ({ ... obj, [property]: (obj [property] || 0) + value }), {}) const list = [{property: 'foo', value: 1}, {property: 'foo', value: 3}, {property: 'bar', value: 2}, {property: 'bar', value: 3}, {property: 'chu', value: 8}] console .log (sumValues (list))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM