Here is the JSON data input provided:
"rows": [
[
1,
"GESTORE_PRATICHE",
1,
"GESTORE PRATICHE",
"canViewFolderManagement"
],
[
2,
"ADM",
1,
"AMMINISTRATORE",
"canViewFolderManagement"
],
[
2,
"ADM",
2,
"AMMINISTRATORE",
"canViewOther"
]
]
The task at hand is to transform this data into a new JSON format using underscorejs as shown below:
[
{
"groupID": "1",
"groupName":"GESTORE_PRATICHE",
"groupDescr":"GESTORE PRATICHE",
"functionList": [
{
"1": "canviewfoldermanagement"
}
]
},
{
"groupID": "2",
"groupName":"ADM",
"groupDescr":"AMMINISTRATORE",
"functionList": [
{
"1": "canviewfoldermanagement",
"2": "canviewOther"
}
]
}
]
To achieve this, an object array with single elements grouped by ID (the first key of each one) is needed. Attempts have been made using underscore js filter and groupby functions but with limited success...
One such attempt was made in angular 2 as seen below:
constructor(private sanitizer: DomSanitizer, private http: Http) {
this.source = new LocalDataSource(this.data); // create the source ;
this.http.get('app/json/profileInput.json')
.subscribe(res => this.data = res.json());
let profileInput;
this.http.get('app/json/profileInput.json')
.subscribe(res =>{
profileInput = res.json()
//console.log(JSON.stringify(profileInput));
this.profileConstructor(profileInput.rows);
}
);
}
profileConstructor(profileRows){
console.log(JSON.stringify(
_.object(JSON.stringify([_.object([profileRows], ['riga'])], [1, 2, 3, 4, 5]))
)
);
} ;