I have an array in the following format
[
{
"duedate": "15-feb-2014",
"alias": "electronus",
"agent": "anonymous",
"name": ""
},
{
"duedate": "15-feb-2014",
"alias": "luminous",
"agent": "anonymous",
"name": ""
},
{
"duedate": "16-feb-2014",
"alias": "GSM",
"agent": "anonymous",
"name": ""
}
]
Underscore group by method allows me to group the array using the properties and in my case using due date. This will return an array like this
{
"15-Feb-2014": [
{
"duedate": "15-feb-2014",
"alias": "electronus",
"agent": "anonymous",
"name": ""
},
{
"duedate": "15-feb-2014",
"alias": "luminous",
"agent": "anonymous",
"name": ""
}
],
"16-Feb-2014": [
{
"duedate": "16-feb-2014",
"alias": "GSM",
"agent": "anonymous",
"name": ""
}
]
}
Is there a possibility that I could have an array like this:
[
{
"duedate": "15-feb-2014",
"myValues": [
{
"duedate": "15-feb-2014",
"alias": "electronus",
"agent": "anonymous",
"name": ""
},
{
"duedate": "15-feb-2014",
"alias": "luminous",
"agent": "anonymous",
"name": ""
}
]
},
{
"duedate": "16-feb-2014",
"myValues": [
{
"duedate": "16-feb-2014",
"alias": "GSM",
"agent": "anonymous",
"name": ""
}
]
}
]
So I could have multiple array values inside each due date so I could parse it to fill a template which I can't change.
I have been struggling with this for quite some time. Any help would be gratefully recieved.
You just need to call map()
after groupBy()
...
var groupedArray = _.chain(list)
.groupBy(function (x) { return x.duedate })
.map(function (value, date) {
return {
duedate: date,
myValues: value
};
})
.value();
JS Fiddle: http://jsfiddle.net/t2tCa/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.