簡體   English   中英

如何使用underscore.js對多個項目進行分組

[英]How to group by on multiple items using 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"
        }
    ]
}
 ]

我想按article_type and brand分組。 誰能幫我。

謝謝

您可以使用_.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> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM