lodash orderBy在嵌套属性上

时间:2016-04-13 18:00:04

标签: lodash

我正在使用v4.11.0。 我想基于milliseconds属性对对象进行排序。 这是阵列:

[
    {
        "name": "bug12755.xml",
        "list": "bugs42",
        "start-date": "2015-09-14",
        "age": {
            "text": "7 months",
            "milliseconds": 18381227304
        }
    },
    {
        "name": "bug12922.xml",
        "list": "bugs42",
        "start-date": "2015-08-27",
        "age": {
            "text": "8 months",
            "milliseconds": 19936427304
        }
    },
    {
        "name": "bug13183.xml",
        "list": "bugs50",
        "start-date": "2015-08-27",
        "age": {
            "text": "8 months",
            "milliseconds": 19936427305
        }
    }
]

我遗漏了iteratee函数的基本内容。我有这个,但似乎没有排序数组。提前谢谢!

 _.orderBy(list, function(item) {
            return item.age.value;
        }, ['desc']);

2 个答案:

答案 0 :(得分:22)

您似乎是通过财产value订购。

u.orderBy(list, function(e) { return e.age.milliseconds}, ['desc']);

答案 1 :(得分:10)

https://codepen.io/a2qube/pen/pKYrgN

关于如何使用Lodash的简单示例:orderBy基于内部属性进行排序。

hotels = _.orderBy(hotels, 'account.id', 'desc');

相关问题