Yury Shapkarin
Yury Shapkarin

Reputation: 519

Lodash groupBy order

I am sorting array by date and then groupBy date but after that first object is 2016 year.

let array = [{date: "2017-06-01", name: 'some'},{date:"2017-05-27",  name: 'someElse'},{date:"2016-12-24", name: 'something'}];
let _data = _.groupBy(array, function(item) {
            return item.date.substring(0,4);
        });

And that output

{2016: [], 2017: []}

And I need

{2017: [], 2016: []}

Upvotes: 0

Views: 2069

Answers (1)

jlaitio
jlaitio

Reputation: 1948

Your result is a JavaScript object with two properties, 2016 and 2017. It is not an array (which are ordered), but an object, whose properties are unordered by design.

You can iterate through them in order by doing something like _.keys(x).sort().reverse().map(...)

Upvotes: 1

Related Questions