1

I have an array like so:

[
  {id: "1", description: "Insurance", date: "2020-12-22T00:00:00Z"},
  {id: "2", description: "Salary", date: "2020-12-20T00:00:00Z"},
  {id: "3", description: "Interest", date: "2020-12-20T00:00:00Z"},
  {id: "4", description: "Panera", date: "2020-12-19T00:00:00Z"},
  {id: "5", description: "Citibank", date: "2020-12-18T00:00:00Z"},
]

Now I want GroupBy date, then sortBy date newest, here is my code, I'm using lodash

const groups = _(list)
  .groupBy((trans) => moment(trans.date).format('MM/DD/YYYY'))
  .sortBy(group => list.indexOf(group[0]))
  .value();

The array result of code above like:

[
  0: [
       {id: "1", description: "Insurance", date: "2020-12-22T00:00:00Z"}
     ],
  1: [
       {id: "2", description: "Salary", date: "2020-12-20T00:00:00Z"},
       {id: "3", description: "Interest", date: "2020-12-20T00:00:00Z"}
     ],
  2: [
       {id: "4", description: "Panera", date: "2020-12-19T00:00:00Z"}
     ]
  
  3: [
       {id: "5", description: "Citibank", date: "2020-12-18T00:00:00Z"}
     ]
]

But I would like the array to return like below:

[
  {
    date: 12/22/2020,
    items: [
       {id: "1", description: "Insurance", date: "2020-12-22T00:00:00Z"}
     ]
  },
  {
    date: 12/20/2020,
    items: [
       {id: "2", description: "Salary", date: "2020-12-20T00:00:00Z"},
       {id: "3", description: "Interest", date: "2020-12-20T00:00:00Z"}
     ],
  },
  {
    date: 12/19/2020,
    items: [
       {id: "4", description: "Panera", date: "2020-12-19T00:00:00Z"}
     ]
  },
  {
    date: 12/18/2020,
    items: [
       {id: "5", description: "Citibank", date: "2020-12-18T00:00:00Z"}
     ]
  }
]

How I can do this with lodash.

Thank for your answer.

2
  • Your desired output is invalid array/object notation (outmost braces should be curly rather than square ones). Commented Jan 25, 2021 at 11:12
  • But I have log array and result like that. Anyway I just have updated my question. :)) Commented Jan 25, 2021 at 11:19

1 Answer 1

1

If vanilla JS solution works for you, you can make use of Array.prototype.reduce() to group your objects and Array.prototype.sort() to sort the output:

const src = [
  {id: "5", description: "Citibank", date: "2020-12-18T00:00:00Z"},
  {id: "1", description: "Insurance", date: "2020-12-22T00:00:00Z"},
  {id: "2", description: "Salary", date: "2020-12-20T00:00:00Z"},
  {id: "3", description: "Interest", date: "2020-12-20T00:00:00Z"},
  {id: "4", description: "Panera", date: "2020-12-19T00:00:00Z"},
],

      result = [...src
        .reduce((acc,o) => {
          const [yyyy, mm, dd] = o.date.split(/[\-T]/),
                key = [mm, dd, yyyy].join('/'),
                group = acc.get(key)
          group
            ? group.items.push(o)
            : acc.set(key, {date: key, items: [o]})
          return acc
        }, new Map)
        .values()]
        .sort(({date:a},{date:b}) => b.localeCompare(a))
        
console.log(result)      
.as-console-wrapper{min-height:100%;}

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.