I have some information saved in the database that I would like to display it in a structure defined by me.
When making a SELECT * FROM table is returned an array of information, as shown below.

Follows the returned JSON to help.
[
{
"IBX": "RJ1",
"GroupName": "N/A",
"ProductName": "Computer Hardware - Server Product",
"POFName": "N/A"
},
{
"IBX": "RJ1",
"GroupName": "N/A",
"ProductName": "Computer Hardware - Server Product",
"POFName": "N/A"
},
{
"IBX": "RJ1",
"GroupName": "TESTE",
"ProductName": "Storage Area Network Product",
"POFName": "N/A"
},
{
"IBX": "RJ1",
"GroupName": "TESTE",
"ProductName": "Storage Area Network Product",
"POFName": "N/A"
},
{
"IBX": "RJ1",
"GroupName": "TESTE",
"ProductName": "Storage Area Network Product",
"POFName": "Teste_1"
},
{
"IBX": "RJ1",
"GroupName": "TESTE",
"ProductName": "Storage Area Network Product",
"POFName": "Teste_1"
},
{
"IBX": "RJ1",
"GroupName": "group_2",
"ProductName": "Computer Hardware - Server Product",
"POFName": "N/A"
},
{
"IBX": "RJ1",
"GroupName": "group_2",
"ProductName": "Computer Hardware - Server Product",
"POFName": "teste_2"
},
{
"IBX": "RJ2",
"GroupName": "TESTE",
"ProductName": "Computer Hardware - Server Product",
"POFName": "teste_3"
},
{
"IBX": "RJ2",
"GroupName": "TESTE",
"ProductName": "Storage Area Network Product",
"POFName": "N/A"
},
{
"IBX": "RJ2",
"GroupName": "TESTE",
"ProductName": "Storage Area Network Product",
"POFName": "N/A"
}
]
I would like to display the information as follows:
What I want is a four-level tree structure.
The first level is the IBX.
The second level is the group name (it is the user that defines it).
The third level is a junction between the product name and the user-given nickname.
The fourth level is the product configuration.
Below is what I tried to do, but I was unsuccessful.
PS.: Bold words represent json key
public transformInNestedList(products: ProductDatabase[]) {
var IBXs = products
.map(x => x.IBX)
.filter((v, i, s) => s.indexOf(v) === i);
var test = IBXs.reduce((a, c) => {
var product_name = products
.filter(x => x.IBX == c)
.map(x => x.ProductName)
.filter((v, i, s) => s.indexOf(v) === i);
console.log(product_name)
var group_name = products
.filter(x => x.IBX == c)
.map(x => x.GroupName)
.filter((v, i, s) => s.indexOf(v) === i);
return a.concat({
IBX: products.find(x => x.IBX == c).IBX,
GROUP_NAMES: group_name.reduce((a2, c2) => a2.concat({
GROUP_NAME: products.find(x => x.IBX == c && x.GroupName == c2).GroupName,
PRODUCTS: product_name.reduce((a3, c3) => a3.concat({
PRODUCT_NAME: products.find(x => x.IBX == c && x.ProductName == c3).ProductName,
ATTRIBUTES: products.filter(x => x.IBX == c && x.ProductName == c3).reduce((a4, c4) => a4.concat({
POE: c4.POE,
ATTRIBUTE: c4.Attributes,
ID: c4.id,
times: c4.times,
Price: c4.Price
}), [])
}), [])
}), [])
})
}, []);
