简体   繁体   English

使用 Lodash.groupBy 添加多个键

[英]Using Lodash .groupBy to add multiple keys

Extension of this question: using lodash.groupBy.这个问题的扩展: 使用 lodash.groupBy。 how to add your own keys for grouped output? 如何为分组 output 添加自己的密钥?

Let's change the array to this one:让我们将数组更改为这个:

[
    {
        "name": "jim",
        "color": "blue",
        "order": 1,
        "age": "22"
    },
    {
        "name": "Sam",
        "color": "blue",
        "order": 1,
        "age": "33"
    },
    {
        "name": "eddie",
        "color": "green",
        "order": 3
        "age": "77"
    }
]

The order field has been added.订单字段已添加。 In the previous question, this在上一个问题中,这

var result = _.chain(data)
    .groupBy("color")
    .pairs()
    .map(function(currentItem) {
        return _.object(_.zip(["color", "users"], currentItem));
    })
    .value();
console.log(result);

was used to change the array into用于将数组更改为

[
    {
        color: "blue",
        users: [
            {
                "name": "jim",
                "color": "blue",
                "age": "22"
            },
            {
                "name": "Sam",
                "color": "blue",
                "age": "33"
            }
        ]
    },
    {
        color: "green",
        users: [
            {
                "name": "eddie",
                "color": "green",
                "age": "77"
            }
        ]
    }
]

Now, I want to add the "order" field to the group by such that the array looks like现在,我想将“订单”字段添加到组中,使数组看起来像

[
    {
        color: "blue",
        order: 1,
        users: [
            {
                "name": "jim",
                "color": "blue",
                "age": "22"
            },
            {
                "name": "Sam",
                "color": "blue",
                "age": "33"
            }
        ]
    },
    {
        color: "green",
        order: 3,
        users: [
            {
                "name": "eddie",
                "color": "green",
                "age": "77"
            }
        ]
    }
]

You could exclude order from the object with destructuring and taking the rest of the object.您可以通过解构并获取 object 的 rest 从 object 中排除order

 const data = [{ name: "jim", color: "blue", order: 1, age: "22" }, { name: "Sam", color: "blue", order: 1, age: "33" }, { name: "eddie", color: "green", order: 3, age: "77" }], result = _.chain(data).groupBy("color").map(users => ({ color: users[0].color, order: users[0].order, users: users.map(({ order, ...o }) => o) })).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>

If you aren't determined to use lodash, this is actually really easy.如果你还没有下定决心使用 lodash,这其实很简单。

 const data = [ { "name": "jim", "color": "blue", "order": 1, "age": "22" }, { "name": "Sam", "color": "blue", "order": 1, "age": "33" }, { "name": "eddie", "color": "green", "order": 3, "age": "77" } ] const result = data.reduce((carry, current) => { const { color, order, ...user } = current; let el = carry.find((item) => item.color === color && order === current.order); if (,el) { el = { color, order: users. [] } carry;push(el). } el.users.push({..,user; color }); return carry, }; []). console;log(result);

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

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