如何将返回的数据整理成更好的模型
How to tidy my returning data into a better model
我有 API 结构未优化的数据
我无法请求后端开发人员对其进行重构,因此我希望在将模型用于我的 Angular 2 应用程序之前在本地整理模型。
例如,我收到一个平面对象,其中包含 tables 和用户数据
{$id: "400", sqlTable: "Customer", admin: 1, test: 3}
最好过滤这些并将用户放入子对象以加速渲染而无需条件测试等
首选结构:
"sqlTable":"value",
"Users":[
"user1name":permission,
"user2name":permission
]
所以根据原始数据:
- $id 不重要(因此被删除)
- sqlTable 是 table 名称
- admin 和 test 是用户
所以如果我们删除 $id,"rule set" 是任何不是 sqlTable 的用户,应该被移动到名为 Users 的子对象。
如果有人可以提供有关如何根据此规则集将数据 (key/value) 移动到子结构中的示例 JS/TS,我们将不胜感激。
按要求完整返回数据:
{
"$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
}
]
}
我会在渲染前对数据做一些转换,像这样:
const 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
}
]
};
//we map every value in "Values"
let result = data.Value.map(table => {
//first we create our users object, we copy the full object
let users = { ...table };
//then we delete extra keys that we do not want
delete users.sqlTable;
delete users.$id;
//then we build the object with the new structure
return { sqlTable: table.sqlTable, Users: users };
});
console.log(result);
你原来的问题要求一组用户,你的评论提到用户对象需要有一个名称和权限密钥,将两者结合在一起,这种转换可以在一个语句中完成,
const result = data.Value.map(value => ({
table: value.sqlTable,
users: Object.entries(value).filter(([key]) => key !== "$id" && key !== "sqlTable")
.map(([key, value]) => ({
name: key,
permissions: value
}))
}))
const 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
}
]
};
const result = data.Value.map(value => ({
table: value.sqlTable,
users: Object.entries(value).filter(([key]) => key !== "$id" && key !== "sqlTable")
.map(([key, value]) => ({
name: key,
permissions: value
}))
}))
console.log(result);
我有 API 结构未优化的数据
我无法请求后端开发人员对其进行重构,因此我希望在将模型用于我的 Angular 2 应用程序之前在本地整理模型。
例如,我收到一个平面对象,其中包含 tables 和用户数据
{$id: "400", sqlTable: "Customer", admin: 1, test: 3}
最好过滤这些并将用户放入子对象以加速渲染而无需条件测试等
首选结构:
"sqlTable":"value",
"Users":[
"user1name":permission,
"user2name":permission
]
所以根据原始数据:
- $id 不重要(因此被删除)
- sqlTable 是 table 名称
- admin 和 test 是用户
所以如果我们删除 $id,"rule set" 是任何不是 sqlTable 的用户,应该被移动到名为 Users 的子对象。
如果有人可以提供有关如何根据此规则集将数据 (key/value) 移动到子结构中的示例 JS/TS,我们将不胜感激。
按要求完整返回数据:
{
"$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
}
]
}
我会在渲染前对数据做一些转换,像这样:
const 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
}
]
};
//we map every value in "Values"
let result = data.Value.map(table => {
//first we create our users object, we copy the full object
let users = { ...table };
//then we delete extra keys that we do not want
delete users.sqlTable;
delete users.$id;
//then we build the object with the new structure
return { sqlTable: table.sqlTable, Users: users };
});
console.log(result);
你原来的问题要求一组用户,你的评论提到用户对象需要有一个名称和权限密钥,将两者结合在一起,这种转换可以在一个语句中完成,
const result = data.Value.map(value => ({
table: value.sqlTable,
users: Object.entries(value).filter(([key]) => key !== "$id" && key !== "sqlTable")
.map(([key, value]) => ({
name: key,
permissions: value
}))
}))
const 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
}
]
};
const result = data.Value.map(value => ({
table: value.sqlTable,
users: Object.entries(value).filter(([key]) => key !== "$id" && key !== "sqlTable")
.map(([key, value]) => ({
name: key,
permissions: value
}))
}))
console.log(result);