0

I have this javascript array of objects for example:

[{
    day : "August 30th",
    id: 1,
    message : "lorem ipsum1"
},{
    day : "August 30th",
    id: 2,
    message : "lorem ipsum2"
},{
    day : "August 31th",
    id: 3,
    message : "lorem ipsum3"
}];

and I need to group it to be object and look like this:

{
    "August 30th": [{
        day : "August 30th",
        id: 1,
        message : "lorem ipsum1"
    },{
        day : "August 30th",
        id: 2,
        message : "lorem ipsum2"
    }],

    "August 31th": [{
        day : "August 31th",
        id: 4,
        message : "lorem ipsum3"
    }]
}

What is the best lodash method to do this?

5
  • 1
    _.groupBy(users, 'day') Commented Aug 31, 2016 at 10:33
  • Phylogenesis thanks Commented Aug 31, 2016 at 10:36
  • 1
    No problem. Unfortunately, this question has been incorrectly marked as a duplicate because of some incorrect terminology in the question (you are asking for your input array to be grouped, rather than sorted). Commented Aug 31, 2016 at 10:44
  • @Phylogenesis, you may answer that question, if you like. Commented Aug 31, 2016 at 11:03
  • 1
    @NinaScholz Thank you. Commented Aug 31, 2016 at 11:07

1 Answer 1

2

As discussed above, you can group your objects in Lo-dash as follows:

_.groupBy(users, 'day');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.