简体   繁体   English

如何按键填充集合数组并对值求和 Lodash.js

[英]How to fill the array of sets by key and sum the values Lodash.js

I am using JS and LoDash library.我正在使用JSLoDash库。

Data that I receive from the server looks like this:我从服务器收到的数据如下所示:

const statsToSort = [
{name: "critA", value: 11111},
{name: "critA", value: 11},
{name: "critA", value: 221},
{name: "critA", value: 54},
{name: "critB", value: 1222},
{name: "critC", value: 231},
{name: "critD", value: 323},
{name: "critC", value: 0},
]

I Need to rearrange it, summ the values for the same keys and reOrganise it.我需要重新排列它,对相同键的值求和并重新组织它。 For this I created the following array:为此,我创建了以下数组:

let finalStats = [{ "critA": 0 }, { "critB": 0 }, { "critC": 0 }, { "CritD": 0 }];

I am trying to use the Lodash library to fill the finalStats jumping of the name and summing the values.我正在尝试使用 Lodash 库来填充名称的 finalStats 跳跃并对值求和。

The function I have so far is like this:我目前的function是这样的:

function mergeNames(arr) {
    return _.chain(arr).groupBy('name').mapValues(function (v) {
        return _.sum(v);
    })

The expected result is supposed to be like this:预期的结果应该是这样的:

/**
statsToSort:[
{name: "critA", value:11397},
{name: "critB", value:1222},
{name: "critC", value:231},
{name: "critD", value:323}, 
*/

So far my function that I have shown does...Nothing.到目前为止,我展示的 function 确实……什么都没有。 Like, at all.就像,完全一样。 It does not even sort the array by keys.它甚至不按键对数组进行排序。

Question: Can you help me问:你能帮帮我吗

  • modify the function to get the result I mentioned修改 function 得到我提到的结果
  • maybe add another layer to the function so that it would do the populating of the finalStats array considering that the names might not be alphabetically arranged and statsToSort might not contain one or few names that are to be populated in the finalStats.可能会向 function 添加另一层,以便考虑到名称可能未按字母顺序排列并且 statsToSort 可能不包含要在 finalStats 中填充的一个或几个名称,它会填充 finalStats 数组。

Thanks in advance提前致谢

You could take a map and get the summed values.您可以使用 map 并获得总和值。

 const statsToSort = [{ name: "critA", value: 11111 }, { name: "critA", value: 11 }, { name: "critA", value: 221 }, { name: "critA", value: 54 }, { name: "critB", value: 1222 }, { name: "critC", value: 231 }, { name: "critD", value: 323 }, { name: "critC", value: 0 }], result = Array.from( statsToSort.reduce((m, { name, value }) => m.set(name, (m.get(name) || 0) + value), new Map), ([name, value]) => ({ name, value }) ).sort((a, b) => a.name.localeCompare(b.name)); console.log(result);

This appears to do what you're after:这似乎可以满足您的要求:

 const statsToSort = [ {name: "critA", value: 11111}, {name: "critA", value: 11}, {name: "critA", value: 221}, {name: "critA", value: 54}, {name: "critB", value: 1222}, {name: "critC", value: 231}, {name: "critD", value: 323}, {name: "critC", value: 0}, ] let mergeNames = arr => _(arr).groupBy('name').mapValues(vals => ({ name: vals[0].name, value: _.sumBy(vals, 'value') })).value() let finalStats = mergeNames(statsToSort) console.log(finalStats)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

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

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