【发布时间】:2020-03-08 06:06:19
【问题描述】:
我有来自 API 的以下 JSON 响应。我的目标是循环遍历 example_terms 数组中的 each term 键并获取所有 $id 值并返回所有值的最终数组,包括 each中的 id > term_parents 数组。所以我的最终结果将类似于:['taxonomy/24232', 'taxonomy/11179', 'taxonomy/12058', 'taxonomy/11053', etc..]。 每个term 对象的深度是可变的,所以我知道我必须以某种方式递归地执行此操作。我不确定如何正确执行此操作,因此感谢您的帮助。
example_terms: [
{
rejected: false,
term: {
$id: "taxonomy/24232",
term_parents: [{
$id: "taxonomy/15197",
term_parents: [{
$id: "taxonomy/11179",
term_parents: [{
$id: "taxonomy/11013"
}]
}]
}],
}
},
{
rejected: false
term: {
$id: "taxonomy/12058",
term_parents: [{
$id: "taxonomy/12110",
term_parents: [{
$id: "taxonomy/12178",
term_parents: [{
$id: "taxonomy/11013"
}]
}]
}],
}
},
{
rejected: false
term: {
$id: "taxonomy/10769",
term_parents: [{
$id: "taxonomy/11401",
term_parents: [{
$id: "taxonomy/11807",
term_parents: [{
$id: "taxonomy/11374",
term_parents: [{
$id: "taxonomy/11053"
}]
}]
}]
}],
}
}
]
【问题讨论】:
标签: javascript arrays recursion