Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

javascript - GroupBy array and then sortBy date that array using lodash

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.

question from:https://stackoverflow.com/questions/65883562/groupby-array-and-then-sortby-date-that-array-using-lodash

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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%;}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...