最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to sort a nested array using lodash - Stack Overflow

programmeradmin0浏览0评论

I have a collection of objects in this array and I need to order them by the 'order' key (asc). Is there a way to sort the objects inside the array and then return the whole array? I am relying on the order as I'm using it in a v-for with a :key.

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

I am currently doing this -

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

The solution above does not seem to order the objects by 'asc' order. Am I on the right track?

Thanks!

P.S. Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. My apologies if I missed an already answered question.

I have a collection of objects in this array and I need to order them by the 'order' key (asc). Is there a way to sort the objects inside the array and then return the whole array? I am relying on the order as I'm using it in a v-for with a :key.

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

I am currently doing this -

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

The solution above does not seem to order the objects by 'asc' order. Am I on the right track?

Thanks!

P.S. Stack is telling me that is a possible duplicate question but I'm at a loss after hours of searching. My apologies if I missed an already answered question.

Share Improve this question edited Nov 24, 2018 at 20:19 Dexygen 12.6k13 gold badges86 silver badges151 bronze badges asked Nov 24, 2018 at 19:51 Raj SahaRaj Saha 491 silver badge7 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 2

You should also consider orderBy method from lodash since you could easily change from asc to desc sort order if you would want to at a later date or have it via a variable being passed through the UI etc:

const data = [ [{ "id": 0, "type": "Header", "order": 1, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "ponent": "header-block" }, { "id": 1, "type": "Header", "order": 0, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "ponent": "header-block" } ], [{ "id": 0, "type": "Header", "order": 2, "props": { "order": 0, "id": 0, "section_id": 0 }, "data": { "header": "" }, "ponent": "header-block" }, { "id": 1, "type": "Header", "order": 1, "props": { "order": 1, "id": 1, "section_id": 0 }, "data": { "header": "" }, "ponent": "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./ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

Just in case you want plain javascript solution.. using Array.forEach

I have also extended your array to contain more data

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

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

console.log(arr)

Will sort each subarray in an array

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

Deep sorting using lodash

const sortedArray = _.orderBy(items, [(item) => {
    const nestedObj = _.get(item, 'props');
    item['props'] = _.orderBy(nestedObj,'order','desc');
  return item['order'];
}], 'desc');
发布评论

评论列表(0)

  1. 暂无评论