Reputation: 11
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
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