如何使用lodash对嵌套数组进行排序

时间:2018-11-24 19:51:47

标签: javascript lodash

我在此数组中有一组对象,我需要通过“ order”键(asc)对其进行排序。有没有一种方法可以对数组内的对象进行排序,然后返回整个数组?我在v-for中使用:key依赖顺序。

[
    {
        "id":0,
        "type":"Header",
        "order":1,
        "props":{
            "order":0,
            "id":0,
            "section_id":0
        },
        "data":{
            "header":""
        },
        "component":"header-block"
    },
    {
        "id":1,
        "type":"Header",
        "order":0,
        "props":{
            "order":1,
            "id":1,
            "section_id":0
         },
         "data":{
            "header":""
         },
         "component":"header-block"
    }
],
[
    //Another collection of objects
]

我目前正在这样做-

getters: {
        sorted: state => {
            return _.orderBy(state.experience_sections, function(block) {
                if(block.experience_blocks[0]) {
                    return block.experience_blocks[0].order;
                }
            });
        }
    }

上面的解决方案似乎没有按'asc'顺序对对象进行排序。我在正确的轨道上吗?

谢谢!

PS 。Stack告诉我这可能是重复的问题,但经过数小时的搜索后,我感到茫然。如果我错过了已经回答的问题,我深表歉意。

4 个答案:

答案 0 :(得分:1)

以防万一,您需要使用Array.forEach

我还扩展了您的数组以包含更多数据

var arr = [[
    {
        "id":0,
        "type":"Header",
        "order":1,
        "props":{
            "order":0,
            "id":0,
            "section_id":0
        },
        "data":{
            "header":""
        },
        "component":"header-block"
    },
    {
        "id":1,
        "type":"Header",
        "order":0,
        "props":{
            "order":1,
            "id":1,
            "section_id":0
         },
         "data":{
            "header":""
         },
         "component":"header-block"
    }
], [
    {
        "id":0,
        "type":"Header",
        "order":2,
        "props":{
            "order":0,
            "id":0,
            "section_id":0
        },
        "data":{
            "header":""
        },
        "component":"header-block"
    },
    {
        "id":1,
        "type":"Header",
        "order":1,
        "props":{
            "order":1,
            "id":1,
            "section_id":0
         },
         "data":{
            "header":""
         },
         "component":"header-block"
    }
]]

arr.forEach(d => d.sort((a,b) => a.order - b.order))

console.log(arr)

答案 1 :(得分:1)

将对数组中的每个子数组进行排序

const sortedArr = _.map(arr, subArray => _.sortBy(subArray, "order"));

答案 2 :(得分:1)

您还应该考虑使用lodash的orderBy方法,因为如果您想在以后的某个日期或通过一个变量将其替换,可以轻松地将asc更改为desc的排序顺序通过UI等传递

const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "component": "header-block" } ] ]

console.log('asc:', _.map(data, x => _.orderBy(x, 'order'))) // asc order
console.log('desc:', _.map(data, x => _.orderBy(x, 'order', 'desc'))) // desc
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

答案 3 :(得分:0)

使用 lodash

进行深层排序
const sortedArray = _.orderBy(items, [(item) => {
    const nestedObj = _.get(item, 'props');
    item['props'] = _.orderBy(nestedObj,'order','desc');
  return item['order'];
}], 'desc');