Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用自定义逻辑进行JSON数据排序_Javascript_Arrays_Sorting_Lodash - Fatal编程技术网

Javascript 使用自定义逻辑进行JSON数据排序

Javascript 使用自定义逻辑进行JSON数据排序,javascript,arrays,sorting,lodash,Javascript,Arrays,Sorting,Lodash,我有一个以下数据JSON数据,我必须按优先级分组,然后按顺序组织分组数据,其中前两个结果为类型1,下一个结果为类型2,下一个结果为类型3,然后重复 { "list": [{ "_id": "555ed01e0f398a7b1436bf05", "type": 1, "priority": 1, "name": "John" }, { "_

我有一个以下数据JSON数据,我必须按优先级分组,然后按顺序组织分组数据,其中前两个结果为类型1,下一个结果为类型2,下一个结果为类型3,然后重复

{
    "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"
        }
    ]
}
我正试图通过使用lodash来做到这一点

_.chain(myArray).groupBy("priority").map(item=> _.groupBy(item,"offerType")).value()
这给了我相应的分组和排序结果,但现在我只能在结果中自定义排序。如何在列表中实现所需的顺序

我正在寻找的结果

 {
    "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"
        }
    ]
   }

从结果来看,在我看来,您只是想对数据进行排序,而不是分组:

result = _.chain(myArray).sortBy("priority", "type")

它不是JSON。请阅读George所说的,它不是JSON,而且,您在此处发布的数组中的对象都有错误,缺少
最后一个类型值无效。@CalvinNunes很抱歉,我更正了JSON@BipinChandraTripathi我不明白您要以何种方式排列阵列的逻辑。是按优先级还是按类型,还是两者都有?我想在组内排序,排序将是前两个结果将是类型1,下一个1将是类型2,然后在组内重复显示的结果数据没有组,只是排序。结果数据正确吗?