【发布时间】:2021-10-14 11:21:04
【问题描述】:
假设我正在尝试将“家谱”创建为一个 JSON 数组。
所以我有一个Person 对象数组。
每个Person 对象都有一个强制name。每个对象还可以选择有一个 children 字段,其中包含一组其他 Person 对象(也有一个 children 字段 - 所以家谱的“深度”基本上可以永远持续下去。)
如果没有孩子,children 字段将只是一个空数组[]。
例如
const family_name = "The Numbers";
const family = [{
name: "1"
children: [],
},
{
name: "2"
children: [{
name: "2 - 1",
children: [],
},
{
name: "2 - 2",
children: [{
name: "2 - 2 - 1",
children: [],
}, ],
}
],
},
{
name: "3"
children: [{
name: "3 - 1",
children: [],
}, ],
},
]
我需要在“孩子”之前发布“父母”。当我发布Person 时,我会在response.data 中获得它的id。此id 需要在直接子项的 POST 中用作parent_id,以便子项与父项关联。
我还需要先发布“family_name”,这将返回family_id。此family_id 将用作parent_id,仅用于最顶层的Persons。
例如
axios.post(FAMILY_URL, {"family_name": family_name})
.then((response) => {
// Promise.all() POST 1, 2, and 3 with their "parent_id" as response.data.family_id
// Promise.all() POST 2-1 and 2-2 with their "parent_id" as 2's response.data.id
// POST 2-2-1 with its "parent_id" as 2-1's response.data.id
// POST 3-1 with its "parent_id" as 3's response.data.id
})
但是如果Person 对象的数量和深度未知,代码会是什么样子?我必须利用递归函数,对吗?
我还想对所有“兄弟姐妹”使用 Promise.all()
【问题讨论】:
-
您需要发布哪些数据?是否需要在帖子数据中包含
children数组? -
尝试查找bfs算法
-
有效负载应该是 {"name": "", "parent_id": ""} 每当通过 POST 创建
Person时,都会返回创建的Person的id。 -
@MikeK.Shum OP 不是 搜索 虽然 ????
-
你需要什么样的最终结果?
标签: javascript recursion axios