javascript - 如何使用 underscore.js 对多个项目进行分组

标签 javascript underscore.js

我有一个长度为 10000 的对象数组。我想操作数据。谁能帮我。

这就是我的数据的样子

[
        {
            "date": "Nov 24, 2015",
            "article_type": "Casual Shoes",
            "brand": "Puma",
            "realised_revenue": "596,882.3",
            "realised_quantity": "289"
        },
        {
            "date": "Nov 23, 2015",
            "article_type": "Casual Shoes",
            "brand": "Puma",
            "realised_revenue": "595,882.3",
            "realised_quantity": "288"
        },
        {
            "article_type": "Sports Shoes",
            "brand": "Puma",
            "date": "Nov 23, 2015",
            "realised_revenue": "394,195.9",
            "realised_quantity": "134"
        },
        {
            "article_type": "Sweatshirts",
            "brand": "Puma",
            "date": "Nov 23, 2015",
            "realised_revenue": "337,385.2",
            "realised_quantity": "191"
        }
    ]

这就是我想要操纵的方式。

    [
{
    groupByRes:{"article_type": "Casual Shoes","brand": "Puma"},
    results:[
        {
            "date": "Nov 24, 2015",
            "article_type": "Casual Shoes",
            "brand": "Puma",
            "realised_revenue": "596,882.3",
            "realised_quantity": "289"
        },
        {
            "date": "Nov 23, 2015",
            "article_type": "Casual Shoes",
            "brand": "Puma",
            "realised_revenue": "595,882.3",
            "realised_quantity": "288"
        }
    ]
},
{
    groupByRes:{"article_type": "Sports Shoes","brand": "Puma"},
    results:[
        {
            "article_type": "Sports Shoes",
            "brand": "Puma",
            "date": "Nov 23, 2015",
            "realised_revenue": "394,195.9",
            "realised_quantity": "134"
        }
    ]
},
{
    groupByRes:{"article_type": "Sweatshirts,"brand": "Puma"},
    results:[
        {
            "article_type": "Sweatshirts",
            "brand": "Puma",
            "date": "Nov 23, 2015",
            "realised_revenue": "337,385.2",
            "realised_quantity": "191"
        }
    ]
}
 ]

我想按文章类型和品牌进行分组。谁能帮助我。

谢谢

最佳答案

您可以使用_.groupBy()“作弊”:

var arr = [
    {
        "date": "Nov 24, 2015",
        "article_type": "Casual Shoes",
        "brand": "Puma",
        "realised_revenue": "596,882.3",
        "realised_quantity": "289"
    },
    {
        "date": "Nov 23, 2015",
        "article_type": "Casual Shoes",
        "brand": "Puma",
        "realised_revenue": "595,882.3",
        "realised_quantity": "288"
    },
    {
        "date": "Nov 23, 2015",
        "article_type": "Casual Shoes",
        "brand": "Not Puma",
        "realised_revenue": "595,882.3",
        "realised_quantity": "288"
    },
    {
        "article_type": "Sports Shoes",
        "brand": "Puma",
        "date": "Nov 23, 2015",
        "realised_revenue": "394,195.9",
        "realised_quantity": "134"
    },
    {
        "article_type": "Sweatshirts",
        "brand": "Puma",
        "date": "Nov 23, 2015",
        "realised_revenue": "337,385.2",
        "realised_quantity": "191"
    }
];

var out = _(arr).groupBy(function (obj) {
    // return a stringified object containing the type and the brand
    return JSON.stringify({ "article_type": obj.article_type, "brand": obj.brand });
}).map(function(value, key) {
    return {
        // parse back the key which is a stringified object
        groupByRes: JSON.parse(key),
        results: value
    }
}).value();
out = JSON.stringify(out, null, 3);
console.log(out);
document.write('<pre>' + out + '</pre>');
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js'></script>

关于javascript - 如何使用 underscore.js 对多个项目进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34429867/

相关文章:

javascript - 如何嵌入带有侧边栏的 youtube 播放列表

javascript - 找出多个数组中的差异。使用减少

node.js - 在node.js中使用npm后从客户端访问underscore.js

javascript - Underscore.js 过滤掉唯一值

php - 我的域名前缀搞乱了...我该如何解决这个问题?

javascript - float如何使用阈值并具有曲线?

javascript - 需要一个文件中的多个模块

javascript - tofixed() + 2 表现得像 tofixed + "2"

underscore.js - 下划线如何使用多个值过滤对象?

javascript - 尝试使用 underscore.js 从该 json 中提取名称