使用 lodash .groupBy.如何为分组输出添加自己的密钥? [英] using lodash .groupBy. how to add your own keys for grouped output?

查看:25
本文介绍了使用 lodash .groupBy.如何为分组输出添加自己的密钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个从 API 返回的示例数据.

我正在使用 Lodash 的 _.groupBy 将数据转换为我可以更好地使用的对象.返回的原始数据是这样的:

<预><代码>[{"name": "吉姆","颜色": "蓝色",年龄":22"},{"name": "山姆","颜色": "蓝色",年龄":33"},{"name": "埃迪","颜色": "绿色",年龄":77"}]

我希望 _.groupBy 函数返回一个如下所示的对象:

<预><代码>[{颜色:蓝色",用户: [{"name": "吉姆","颜色": "蓝色",年龄":22"},{"name": "山姆","颜色": "蓝色",年龄":33"}]},{颜色:绿色",用户: [{"name": "埃迪","颜色": "绿色",年龄":77"}]}]

目前我正在使用

_.groupBy(a, function(b) { return b.color})

这是返回这个.

{蓝色:[{..}],绿色:[{...}]}

分组是正确的,但我真的很想添加我想要的键(colorusers).这可以使用 _.groupBy 吗?或其他一些 LoDash 实用程序?

解决方案

你可以在 Underscore 和 Lodash(3.x 和 4.x)中这样做.

var data = [{"name": "吉姆","颜色": "蓝色",年龄":22"}, {"name": "山姆","颜色": "蓝色",年龄":33"}, {"name": "埃迪","颜色": "绿色",年龄":77"}];控制台日志(_.chain(数据)//根据 `color` 属性对 Array 的元素进行分组.groupBy("颜色")//`key` 是组名(颜色),`value` 是对象数组.map((value, key) => ({ color: key, users: value })).价值());

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>


原答案

var 结果 = _.chain(data).groupBy(颜色").对().map(function(currentItem) {返回 _.object(_.zip([color", users"], currentItem));}).价值();控制台日志(结果);

在线演示

注意:从 Lodash 4.0 开始,.pairs 函数已重命名为 _.toPairs()

I have this sample data returned from an API.

I'm using Lodash's _.groupBy to convert the data into an object I can use better. The raw data returned is this:

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

I want the _.groupBy function to return an object that looks like this:

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

Currently I'm using

_.groupBy(a, function(b) { return b.color})

which is returning this.

{blue: [{..}], green: [{...}]}

the groupings are correct, but I'd really like to add the keys I want (color, users). is this possible using _.groupBy? or some other LoDash utility?

解决方案

You can do it like this in both Underscore and Lodash (3.x and 4.x).

var data = [{
  "name": "jim",
  "color": "blue",
  "age": "22"
}, {
  "name": "Sam",
  "color": "blue",
  "age": "33"
}, {
  "name": "eddie",
  "color": "green",
  "age": "77"
}];

console.log(
  _.chain(data)
    // Group the elements of Array based on `color` property
    .groupBy("color")
    // `key` is group's name (color), `value` is the array of objects
    .map((value, key) => ({ color: key, users: value }))
    .value()
);

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js"></script>


Original Answer

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

Online Demo

Note: Lodash 4.0 onwards, the .pairs function has been renamed to _.toPairs()

这篇关于使用 lodash .groupBy.如何为分组输出添加自己的密钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆