I need to restructure API data that is not optimized in its current format
Unfortunately, I am unable to ask backend developers to make the necessary changes, so I am exploring ways to clean up the model locally before using it in my Angular 2 application.
For instance, the data I receive is a flat object containing tables and user information
{$id: "400", sqlTable: "Customer", admin: 1, test: 3}
It is more efficient to filter this data and place users into a sub-object in order to speed up rendering without the need for conditional testing.
Desired structure:
"sqlTable":"value",
"Users":[
"user1name":permission,
"user2name":permission
]
From the original data:
- $id is not essential (thus, it was removed)
- sqlTable represents the table name
- admin and test fields are considered as Users
Therefore, the "rule set" is that anything other than sqlTable is classified as a User and should be transferred to a Sub Object called Users.
I would greatly appreciate it if someone could provide an example in JS/TS on how to move the key/value data into the sub-structure based on these rules.
Here is the complete returned data:
{
"$id":"399",
"IsFailure":false,
"IsSuccess":true,
"Value":[
{
"$id":"400",
"sqlTable":"Customer",
"admin":1,
"test":null
},
{
"$id":"401",
"sqlTable":"CustomerAddress",
"admin":null,
"test":null
},
{
"$id":"402",
"sqlTable":"NewTable",
"admin":null,
"test":null
}
]
}