【发布时间】:2020-10-05 04:26:27
【问题描述】:
我有点被这个问题困扰:
我有这个 JSON:
[
{
"id": 1,
"name": "Sales",
"superdepartment": null
},
{
"id": 2,
"name": "Engineering",
"superdepartment": null
},
{
"id": 3,
"name": "Product",
"superdepartment": null
},
{
"id": 4,
"name": "Design",
"superdepartment": 3
},
{
"id": 5,
"name": "Inbound Sales",
"superdepartment": 1
},
{
"id": 6,
"name": "Outbound Sales",
"superdepartment": 1
},
{
"id": 7,
"name": "Application Security",
"superdepartment": 2
},
{
"id": 8,
"name": "Front-End",
"superdepartment": 2
},
{
"id": 9,
"name": "Sales Development",
"superdepartment": 6
},
{
"id": 10,
"name": "Product Management",
"superdepartment": 3
}
]
因此,我需要根据所需的级别递归地扩展“超级部门”关系。例如:
- 如果我将 ?expand=superdeparment 传递给我的端点,我需要打开 1 级关系
- 如果我通过 ?expand=superdepartment.superdepartment 我需要打开 2 个级别,并且可以继续进行,所以我认为我需要一个递归解决方案。
实际上,我有满足第一级的这段代码,但是我在替换嵌套对象以打开第二级关系时遇到了几个问题。
departments.js --> 这里我获取数据(json)并调用“getRelations”方法。
module.exports.getAll = async function getAll(expand = null) {
let response = await data;
if (expand) {
response = modelUtils.getRelations(response, expand, response);
}
return response;
}
modelUtils.js --> 在这里我编写了我的核心函数来实现嵌套对象:
const _ = require('lodash');
//targetEntity is de JSON that I will use to get the nested entities from my actual ID.
// In this case is the same json, but can be another different.
module.exports.getRelations = function getRelations(entity, expand, targetEntity) {
let tmpEntity = _.cloneDeep(entity);
let path = expand.split('.');
for (let i=0; i < entity.length; i++) {
tmpEntity[i] = fillRelations(entity[i], path, targetEntity);
}
return tmpEntity;
}
function fillRelations(entity, path, targetEntity, level = 0) {
let current = _.cloneDeep(entity);
const currentPath = path[level];
if (!current[currentPath]) {
return current;
}
let value = targetEntity.filter(target => target.id === current[currentPath]);
if (value.length > 0) {
current[currentPath] = value[0];
}
level++;
return fillRelations(current, path, targetEntity, level);
}
所以实际上使用这段代码并将 ?expand=superdepartment.superdepartment 传递到我的端点,我得到了这个 JSON 响应:
[
{
"id": 1,
"name": "Sales",
"superdepartment": null
},
{
"id": 2,
"name": "Engineering",
"superdepartment": null
},
{
"id": 3,
"name": "Product",
"superdepartment": null
},
{
"id": 4,
"name": "Design",
"superdepartment": {
"id": 3,
"name": "Product",
"superdepartment": null
}
},
{
"id": 5,
"name": "Inbound Sales",
"superdepartment": {
"id": 1,
"name": "Sales",
"superdepartment": null
}
},
{
"id": 6,
"name": "Outbound Sales",
"superdepartment": {
"id": 1,
"name": "Sales",
"superdepartment": null
}
},
{
"id": 7,
"name": "Application Security",
"superdepartment": {
"id": 2,
"name": "Engineering",
"superdepartment": null
}
},
{
"id": 8,
"name": "Front-End",
"superdepartment": {
"id": 2,
"name": "Engineering",
"superdepartment": null
}
},
{
"id": 9,
"name": "Sales Development",
"superdepartment": {
"id": 6,
"name": "Outbound Sales",
"superdepartment": 1
}
},
{
"id": 10,
"name": "Product Management",
"superdepartment": {
"id": 3,
"name": "Product",
"superdepartment": null
}
}
]
如你所见,ID=9 的元素需要为 id=1 打开第二层嵌套关系,所以它必须是这样的:
{
"id": 9,
"name": "Sales Development",
"superdepartment": {
"id": 6,
"name": "Outbound Sales",
"superdepartment": {
"id": 1,
"name": "Sales",
"superdepartment": null
}
}
},
【问题讨论】:
标签: javascript node.js rest recursion expand