0

I have a following data JSON data which I have to group by priority then organise the grouped data in order where first two result will be type 1 and next 1 of type 2 and next 1 will be type 3 and then repeat

{
    "list": [{
            "_id": "555ed01e0f398a7b1436bf05",
            "type": 1,
            "priority": 1,
            "name": "John"
        },
        {
            "_id": "555ed01e0f398a7b1436bf07",
            "type": 2,
            "priority": 1,
            "name": "Jack"
        },
        {
            "_id": "555ed01e0f398a7b1436bf09",
            "type": 1,
            "priority": 1,
            "name": "Smith"
        },
        {
            "_id": "555ed01e0f398a7b1436bf09",
            "type": 2,
            "priority": 1,
            "name": "Kiles"
        },
        {
            "_id": "555ed01e0f398a7b1436bf11",
            "type": 1,
            "priority": 1,
            "name": "Stacy"
        },
        {
            "_id": "555ed01e0f398a7b1436bf13",
            "type": 1,
            "priority": 2,
            "name": "Stacy"
        }
    ]
}

I am trying to do this by using lodash

_.chain(myArray).groupBy("priority").map(item=> _.groupBy(item,"offerType")).value()

This give me grouped and sorted result accordingly but now I am stuck with custom ordering in the results. How to achieve the desired order in list.

The result I am looking for

 {
    "list": [{
            "_id": "555ed01e0f398a7b1436bf05",
            "type": 1,
            "priority": 1,
            "name": "John"
        },
        {
            "_id": "555ed01e0f398a7b1436bf09",
            "type": 1,
            "priority": 1,
            "name": "Smith"
        },
        {
            "_id": "555ed01e0f398a7b1436bf07",
            "type": 2,
            "priority": 1,
            "name": "Jack"
        },
        {
            "_id": "555ed01e0f398a7b1436bf11",
            "type": 1,
            "priority": 1,
            "name": "Stacy"
        },
        {
            "_id": "555ed01e0f398a7b1436bf09",
            "type": 2,
            "priority": 1,
            "name": "Kiles"
        },
        {
            "_id": "555ed01e0f398a7b1436bf13",
            "type": 2,
            "priority": 2,
            "name": "Stacy"
        }
    ]
   }
Bipin Chandra Tripathi
  • 2,550
  • 4
  • 28
  • 45
  • 2
    It is not JSON. – Dexygen Aug 02 '18 at 12:19
  • Please read [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – str Aug 02 '18 at 12:21
  • as George said, it is not JSON, and also, the objects inside the array you posted here, are all with errors, missing `,` ,`:` and a invalid type value in the last one. – Calvin Nunes Aug 02 '18 at 12:26
  • @CalvinNunes sorry for that I corrected the JSON – Bipin Chandra Tripathi Aug 02 '18 at 12:34
  • @str thanks ! I added that in hurry which I have corrected – Bipin Chandra Tripathi Aug 02 '18 at 12:35
  • @GeorgeJempty thanks I corrected that – Bipin Chandra Tripathi Aug 02 '18 at 12:36
  • 1
    @BipinChandraTripathi I don't understand the logic in which way you want to order the array. Is by priority or by type or both of them? Doesn't look like is ordered to me your desired outcome. – NickHTTPS Aug 02 '18 at 12:42
  • @NickHTTPS I just want to reorder the objects in array with criteria which is first group them with priority so that same priority are in one group then sort them within group in such a way that first two items are of type 1 and next one of type 2 and repeat within the group and same for the other groups and finally the returned result as array of objects – Bipin Chandra Tripathi Aug 02 '18 at 12:54

1 Answers1

0

Looking at the result it seems to me that you just want to sort, not group, your data:

result = _.chain(myArray).sortBy("priority", "type")
bjelli
  • 9,752
  • 4
  • 35
  • 50