繁体   English   中英

如何通过键减少对象数组并在javascript中求和

[英]How to reduce an array of objects by key and sum its values in javascript

我试图扭转这个:

var crates = [{category: "fruits", type: "apple", color: "green", number: 1}, 
             {category: "fruits", type: "apple", color: "red", number: 1},
             {category: "fruits", type: "banana", color: "yellow", number: 1}, 
             {category: "vegetables", type: "onion", color: "white", number: 1}]

到这个:

var stand = [{category: "fruits", type: "apple", sum: 2}, 
             {category: "fruits", type: "banana", sum: 1}, 
             {category: "vegetables", type: "onion", sum: 1}]

使用lodash / fp。 到目前为止,我已经尝试了很多,这是我设法获得的最接近的结果:

var stand = flow(
      groupBy('type'),
      map((objs, key) => ({
          'category': key,
          'type': key,
          'sum': _.sumBy(objs, 'number') }))
    )(crates);

结果是:

[{category: undefined, type: undefined, sum: 2},
{category: undefined, type: undefined, sum: 1}
{category: undefined, type: undefined, sum: 1}]

显然,我不知道如何将对“ category”和“ type”值的引用传递给map函数。

我是不熟悉lodash / fp的人,并且一直在与整个fp概念作斗争,所以我为可能为我指明正确方向的任何事情感到高兴!

在lodash-fp方法中,固定系数为1(回调接收1个参数)以支持自动循环。 这意味着map的回调没有获取密钥。 您可以从组中的第一个对象获取typecategory

以允许的讨好sumBy()参数被切换,所以objs应该是第二PARAM:

 const { flow, groupBy, map, sumBy } = _; const crates = [{"category":"fruits","type":"apple","color":"green","number":1},{"category":"fruits","type":"apple","color":"red","number":1},{"category":"fruits","type":"banana","color":"yellow","number":1},{"category":"vegetables","type":"onion","color":"white","number":1}]; const result = flow( groupBy('type'), map((objs) => ({ 'category': objs[0].category, 'type': objs[0].type, 'sum': sumBy('number', objs) })) )(crates); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash-fp/0.10.4/lodash-fp.js"></script> 

这是我使用Array.reduce和Array.findIndex的解决方案。

 var crates = [{ category: "fruits", type: "apple", color: "green", number: 1 }, { category: "fruits", type: "apple", color: "red", number: 1 }, { category: "fruits", type: "banana", color: "yellow", number: 1 }, { category: "vegetables", type: "onion", color: "white", number: 1 }] console.log( crates.reduce( (crates, item) => { const index = crates.findIndex(i => { return i.type == item.type && i.category == item.category } ) if (index > -1) crates[index].sum += item.number else { crates.push({ type: item.type, category: item.category, sum: item.number }) } return crates; }, []) ); 

尽管它比lodash更为冗长,但学习标准数组方法来解决问题将使您对第三方库的依赖性降低。

作为初学者,我在@mpm上工作:认为这是编写您自己的lodash实用程序版本的实践机会。 这是一个MASSIVE库,您可能不会充分利用它来证明将其添加到package.json中的依赖项是合理的。 这是又分为两个步骤的另一种方法,首先将输入数组分解为一个对象(具有各种水果类型),然后将新对象压入输出数组:

 const standMaker = (crates) => { var fruits = {}; for (let fruit of crates) { if (!fruits[fruit.type]) { fruits[fruit.type] = {}; fruits[fruit.type].sum = 1; fruits[fruit.type].category = fruit.category; } else if (fruits[fruit.type]) { fruits[fruit.type].sum += 1; } }; var stand = []; for (let fruit in fruits) { stand.push({category: fruits[fruit].category, type: fruit, sum: fruits[fruit].sum}); } console.log(stand); } 

暂无
暂无

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

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