Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort objects in array by key with lodash

Hi Stack Overflow community!

I have the following test array:

let test = [{
    "searchQuery": "test",
    "resultsLoaded": -1,
    "offset": -1,
    "allResults": 10
}, {
    "metaData": {
        "type": "page-content",
        "image": true,
        "name": "lorem-ipsum",
        "rank": 10,
        "tags": ["website:lorem/ipsum"]
    },
    "componentData": {
        "header": "Lorem Ipsum",
        "path": "/content/asdf",
        "breadcrumbDouble": {
            "breadcrumbItem1": {
                "href": "lorem ipsum",
                "text": "lorem ipsum"
            },
            "breadcrumbItem2": {
                "href": "/content/asdf",
                "text": "Lorem Ipsum"
            }
        },
        "content": {
            "subheadline": "Lorem Ipsum",
            "img": {
                "alt": "",
                "imgSrc": "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg"
            },
            "tags": [{
                "category": "lorem",
                "value": "ipsum"
            }]
        }
    }
}, {
    "metaData": {
        "type": "page-content",
        "image": true,
        "name": "lorem-ipsum",
        "rank": 50,
        "tags": ["website:lorem/ipsum"]
    },
    "componentData": {
        "header": "Lorem Ipsum",
        "path": "/content/asdf",
        "breadcrumbDouble": {
            "breadcrumbItem1": {
                "href": "lorem ipsum",
                "text": "lorem ipsum"
            },
            "breadcrumbItem2": {
                "href": "/content/asdf",
                "text": "Lorem Ipsum"
            }
        },
        "content": {
            "subheadline": "Lorem Ipsum",
            "img": {
                "alt": "",
                "imgSrc": "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg"
            },
            "tags": [{
                "category": "lorem",
                "value": "ipsum"
            }]
        }
    }
}, {
    "metaData": {
        "type": "page-content",
        "image": true,
        "name": "lorem-ipsum",
        "rank": 30,
        "tags": ["website:lorem/ipsum"]
    },
    "componentData": {
        "header": "Lorem Ipsum",
        "path": "/content/asdf",
        "breadcrumbDouble": {
            "breadcrumbItem1": {
                "href": "lorem ipsum",
                "text": "lorem ipsum"
            },
            "breadcrumbItem2": {
                "href": "/content/asdf",
                "text": "Lorem Ipsum"
            }
        },
        "content": {
            "subheadline": "Lorem Ipsum",
            "img": {
                "alt": "",
                "imgSrc": "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg"
            },
            "tags": [{
                "category": "lorem",
                "value": "ipsum"
            }]
        }
    }
}];

What I want to achive now is to sort the objects by metaData.rank from highest to lowest.

I wanted to use lodash's orderBy() like this:

let result = orderBy(test, function(e) {
    let array = parseInt(e.metaData.rank);
    return array;
}, ['desc']);

But I get the following error:

TypeError: Cannot read property 'rank' of undefined

console.log(e) in my orderBy function shows me this:

{
    searchQuery: 'test',
    resultsLoaded: -1,
    offset: -1,
    allResults: 10
}

Any quick suggestions to fix this?

I know that the first object is causing the problem because of the different structure. So I need to work around it!

like image 771
mrks Avatar asked Sep 10 '26 10:09

mrks


1 Answers

You could move the items without metaData property to top and sort the rest by rank.

var array = [{ searchQuery: "test", resultsLoaded: -1, offset: -1, allResults: 10 }, { metaData: { type: "page-content", image: true, name: "lorem-ipsum", rank: 10, tags: ["website:lorem/ipsum"] }, componentData: { header: "Lorem Ipsum", path: "/content/asdf", breadcrumbDouble: { breadcrumbItem1: { href: "lorem ipsum", text: "lorem ipsum" }, breadcrumbItem2: { href: "/content/asdf", text: "Lorem Ipsum" } }, content: { subheadline: "Lorem Ipsum", img: { alt: "", imgSrc: "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg" }, tags: [{ category: "lorem", value: "ipsum" }] } } }, { metaData: { type: "page-content", image: true, name: "lorem-ipsum", rank: 50, tags: ["website:lorem/ipsum"] }, componentData: { header: "Lorem Ipsum", path: "/content/asdf", breadcrumbDouble: { breadcrumbItem1: { href: "lorem ipsum", text: "lorem ipsum" }, breadcrumbItem2: { href: "/content/asdf", text: "Lorem Ipsum" } }, content: { subheadline: "Lorem Ipsum", img: { alt: "", imgSrc: "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg" }, tags: [{ category: "lorem", value: "ipsum" }] } } }, { metaData: { type: "page-content", image: true, name: "lorem-ipsum", rank: 30, tags: ["website:lorem/ipsum"] }, componentData: { header: "Lorem Ipsum", path: "/content/asdf", breadcrumbDouble: { breadcrumbItem1: { href: "lorem ipsum", text: "lorem ipsum" }, breadcrumbItem2: { href: "/content/asdf", text: "Lorem Ipsum" } }, content: { subheadline: "Lorem Ipsum", img: { alt: "", imgSrc: "/content/dam/production/images/asdf.jpeg.imgTransformer/listPageItem/desktop/1511962617778/asdf.jpg" }, tags: [{ category: "lorem", value: "ipsum" }] } } }];

array.sort(function (a, b) {
    return ('metaData' in a) - ('metaData' in b) || b.metaData.rank - a.metaData.rank;
});

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 114
Nina Scholz Avatar answered Sep 12 '26 01:09

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!