Nhân Lê
Nhân Lê

Reputation: 11

Lodash: Group and restructure the json object

How can I group and restructure the JSON object using lodash. I have a JSON object like this:

[
  {
    "time":"2021-03-30T09:00:29.000Z",
    "ngay":"2021-03-30",
    "status":0
  },
  {
    "time":"2021-03-30T09:00:38.000Z",
    "ngay":"2021-03-30",
    "status":1
  }
]

I want to group it like this:

{
  "2021-03-30": [
    {
      "time":"2021-03-30T09:00:29.000Z",
      "ngay":"2021-03-30",
      "status":0
    },
    {
      "time":"2021-03-30T09:00:38.000Z",
      "ngay":"2021-03-30",
      "status":1
    }
  ]
}

Upvotes: 0

Views: 64

Answers (1)

CyberEternal
CyberEternal

Reputation: 2545

You can do it in the following way

const list = [
  {
    "time":"2021-03-30T09:00:29.000Z",
    "ngay":"2021-03-30",
    "status":0
  },
  {
    "time":"2021-03-30T09:00:38.000Z",
    "ngay":"2021-03-30",
    "status":1
  }
]

const gropedList = _.groupBy(list, 'ngay')

For more information please check the Lodash documentation

Upvotes: 1

Related Questions