Lodash过滤器唯一然后分组对象

时间:2017-09-11 13:30:29

标签: javascript lodash

var data = [
   {
      "userCity": "Uttar Pradesh ",
      "userState": "Uttar Pradesh ",
      "AssetId": 53,
      "AssetName": "Parent ID",
      "AssetInfo": "{\"uuid\":\"37fb6308-b962-41da-b08f-122ae124500a\",\"field\":\"file\",\"file\":\"public/37fb6308-b962-41da-b08f-122ae124500a/file/maxresdefault.jpg\",\"filename\":\"maxresdefault.jpg\",\"encoding\":\"7bit\",\"mimetype\":\"image/jpeg\",\"truncated\":false,\"done\":true}",
      "AssetSize": "0.088",
      "CreatedAt": "2017-09-11T04:00:00.000Z",
      "AssetType": "image/jpeg",
      "AssetLocation": "https://ampretailer-prod.s3.amazonaws.com/1505129969436.jpg"
   },
   {
      "userCity": "New Delhi",
      "userState": "Delhi",
      "AssetId": 54,
      "AssetName": "PARENT ID TEST",
      "AssetInfo": "{\"uuid\":\"150651f1-81be-4059-87b4-f2727f2c5ad6\",\"field\":\"file\",\"file\":\"public/150651f1-81be-4059-87b4-f2727f2c5ad6/file/58832_300x300.jpg\",\"filename\":\"58832_300x300.jpg\",\"encoding\":\"7bit\",\"mimetype\":\"image/jpeg\",\"truncated\":false,\"done\":true}",
      "AssetSize": "0.004",
      "CreatedAt": "2017-09-11T04:00:00.000Z",
      "AssetType": "image/jpeg",
      "AssetLocation": "https://ampretailer-prod.s3.amazonaws.com/1505132244210.jpg"
   },
   {
      "userCity": "Uttar Pradesh ",
      "userState": "Uttar Pradesh ",
      "AssetId": 55,
      "AssetName": "Parent ID ",
      "AssetInfo": "{\"uuid\":\"253270a5-f987-445c-959a-be1ba057f7f8\",\"field\":\"file\",\"file\":\"public/253270a5-f987-445c-959a-be1ba057f7f8/file/nodejs-new-pantone-black.png\",\"filename\":\"nodejs-new-pantone-black.png\",\"encoding\":\"7bit\",\"mimetype\":\"image/png\",\"truncated\":false,\"done\":true}",
      "AssetSize": "0.045",
      "CreatedAt": "2017-09-11T04:00:00.000Z",
      "AssetType": "image/png",
      "AssetLocation": "https://ampretailer-prod.s3.amazonaws.com/1505132375228.png"
   }
]

我希望输出具有唯一的AssetId,然后按userCity,userState

排序
data = [
    city : userCity,
     state : userState ,
     assets:{all details }
]

如何使用lodash我可以使用_.groupBy但不会获得欲望输出

我试过

 users = _.uniqBy(data,'AssetId');
 result = _.groupBy(users, function(o) { return o.userCity; });

输出

{
   "Uttar Pradesh ": [
      {
         "userCity": "Uttar Pradesh ",
         "userState": "Uttar Pradesh ",
         "AssetId": 53,
         "AssetName": "Parent ID",
         "AssetInfo": "{\"uuid\":\"37fb6308-b962-41da-b08f-122ae124500a\",\"field\":\"file\",\"file\":\"public/37fb6308-b962-41da-b08f-122ae124500a/file/maxresdefault.jpg\",\"filename\":\"maxresdefault.jpg\",\"encoding\":\"7bit\",\"mimetype\":\"image/jpeg\",\"truncated\":false,\"done\":true}",
         "AssetSize": "0.088",
         "CreatedAt": "2017-09-11T04:00:00.000Z",
         "AssetType": "image/jpeg",
         "AssetLocation": "https://ampretailer-prod.s3.amazonaws.com/1505129969436.jpg"
      },
      {
         "userCity": "Uttar Pradesh ",
         "userState": "Uttar Pradesh ",
         "AssetId": 55,
         "AssetName": "Parent ID ",
         "AssetInfo": "{\"uuid\":\"253270a5-f987-445c-959a-be1ba057f7f8\",\"field\":\"file\",\"file\":\"public/253270a5-f987-445c-959a-be1ba057f7f8/file/nodejs-new-pantone-black.png\",\"filename\":\"nodejs-new-pantone-black.png\",\"encoding\":\"7bit\",\"mimetype\":\"image/png\",\"truncated\":false,\"done\":true}",
         "AssetSize": "0.045",
         "CreatedAt": "2017-09-11T04:00:00.000Z",
         "AssetType": "image/png",
         "AssetLocation": "https://ampretailer-prod.s3.amazonaws.com/1505132375228.png"
      }
   ],
   "New Delhi": [
      {
         "userCity": "New Delhi",
         "userState": "Delhi",
         "AssetId": 54,
         "AssetName": "PARENT ID TEST",
         "AssetInfo": "{\"uuid\":\"150651f1-81be-4059-87b4-f2727f2c5ad6\",\"field\":\"file\",\"file\":\"public/150651f1-81be-4059-87b4-f2727f2c5ad6/file/58832_300x300.jpg\",\"filename\":\"58832_300x300.jpg\",\"encoding\":\"7bit\",\"mimetype\":\"image/jpeg\",\"truncated\":false,\"done\":true}",
         "AssetSize": "0.004",
         "CreatedAt": "2017-09-11T04:00:00.000Z",
         "AssetType": "image/jpeg",
         "AssetLocation": "https://ampretailer-prod.s3.amazonaws.com/1505132244210.jpg"
      }
   ]
}

1 个答案:

答案 0 :(得分:1)

  

尝试此操作,它会根据需要提供输出

let assets = [];
result = _(results)
         .uniqBy('AssetId')
         .map(function(d){d.city_state = d.userCity +''+d.userState; return d;})
         .groupBy('city_state')
         .forOwn(function(v,k){
              assets.push(v);
         });