I have a JSON object as below upComingArray":
[
{
"companyAlias": "ABC",
"refNo": "ABC0001",
"Date": "12/09/2018",
"Sub": "ABC Subj" ...
},
{
"companyAlias": "ABC",
"refNo": "ABC0002",
"Date": "12/09/2018",
"Sub": "ABC Subj1" ...
},
{
"companyAlias": "ABC",
"refNo": "ABC0003",
"Date": "12/09/2018",
"Sub": "ABC Subj3"...
},
{
"companyAlias": "BCD",
"refNo": "BCD0001",
"Date": "14/09/2018",
"Sub": "BCD Subj"...
},
{
"companyAlias": "BCD",
"refNo": "BCD0002",
"Date": "14/09/2018",
"Sub": "BCD Subj2"...
},...
]
I would like to convert it to the below format for ease processing (basically group by companyAlias and Date)
upComingArray":
[
{
"companyAlias": "ABC",
"Date": "12/09/2018"
[ {
"refNo": "ABC0001"
"Sub": "ABC Subj"
},
{
"refNo": "ABC0002",
"Sub": "ABC Subj1"
},
{
"refNo": "ABC0003",
"Sub": "ABC Subj3"
}]
},
{
"companyAlias": "BCD",
"Date": "14/09/2018"
[
{
"refNo": "BCD0001",
"Sub": "BCD Subj"
},
{
"refNo": "BCD0002",
"Sub": "BCD Subj2"
}]
},...
]
If anyone have idea, Please let me know...
Thanks in Advance
The below code is working.
var inputArray = [
{
"companyAlias": "ABC",
"refNo": "ABC0001",
"Date": "12/09/2018",
"Sub": "ABC Subj" ...
},
{
"companyAlias": "ABC",
"refNo": "ABC0002",
"Date": "12/09/2018",
"Sub": "ABC Subj1" ...
},
{
"companyAlias": "ABC",
"refNo": "ABC0003",
"Date": "12/09/2018",
"Sub": "ABC Subj3"...
},
{
"companyAlias": "BCD",
"refNo": "BCD0001",
"Date": "14/09/2018",
"Sub": "BCD Subj"...
},
{
"companyAlias": "BCD",
"refNo": "BCD0002",
"Date": "14/09/2018",
"Sub": "BCD Subj2"...
}
]
var resultArray = groupByMultiple(inputArray ,function(item)
{
return [item.companyAlias, item.Date];
});
function groupByMultiple( array , f )
{
var groups = {};
array.forEach( function( o )
{
var group = JSON.stringify( f(o) );
groups[group] = groups[group] || [];
groups[group].push( o );
});
return Object.keys(groups).map( function( group )
{
return groups[group];
})
}