提问人:ganesh 提问时间:10/7/2022 最后编辑:ganesh 更新时间:10/7/2022 访问量:307
TypeScript:遍历嵌套的对象数组,直到满足条件
typescript: traverse nested array of objects until condition is met
问:
我有如下所示的嵌套数组。
const tree = {
"id": 1,
"name": "mainOrgName",
"children": [
{
"id": 10,
"name": "East Region",
"children": [
{
"id": 11,
"name": "test east sub 1",
"children": [
{
"id": 12,
"name": "sub 22 sub 1",
"children": [
{
"id": 15,
"name": "sub 333 of sub ",
"children": [
{
"id": 16,
"name": "sub 4444",
"children": []
}
]
}
]
}
]
}
]
},
{
"id": 13,
"name": "west region",
"children": [
{
"id": 14,
"name": "test west sub 1",
"children": []
}
]
}
]
}
我需要遍历数组以获取子数组及其子数组,直到我们没有发现子数组为空。(注意:子数组可能为空或可能有更多级别)tree.children
all id and name
我需要如下结果 预期结果
[
{
"name": "East Region",
"value": 10,
"selected": false
},
{
"name": "test east sub 1",
"value": 11,
"selected": false
},
{
"name": "sub 22 sub 1",
"value": 12,
"selected": false
},
{
"name": "sub 333 of sub",
"value": 15,
"selected": false
},
{
"name": "sub 4444",
"value": 16,
"selected": false
},
{
"name": "west region",
"value": 13,
"selected": false
},
{
"name": "test west sub 1",
"value": 14,
"selected": false
},
]
我试着跟着
const candidates = tree.children.map(org => ({name: org.name, value: org.id, selected: false}));
但它给了我追随者
[
{
"name": "East Region",
"value": 10,
"selected": false
},
{
"name": "west region",
"value": 13,
"selected": false
}
]
我正在尝试得到它,但不确定如何放置遍历直到子项为空的条件,并以所需的格式在最终数组中推送必填字段。可能需要递归/回调函数,但不确定如何使用它。
请帮助获得预期的结果。谢谢
答:
1赞
Kiddo
10/7/2022
#1
试试这个,
const tree = {
"id": 1,
"name": "mainOrgName",
"children": [
{
"id": 10,
"name": "East Region",
"children": [{
"id": 11,
"name": "test east sub 1",
"children": [{
"id": 12,
"name": "sub 22 sub 1",
"children": [{
"id": 15,
"name": "sub 333 of sub ",
"children": [{
"id": 16,
"name": "sub 4444",
"children": []
}]
}]
}]
}]
},
{
"id": 13,
"name": "west region",
"children": [{
"id": 14,
"name": "test west sub 1",
"children": []
}]
}
]
}
let items = []
let result = lootIt(tree.children)
console.log(result)
function lootIt (arr) {
for(let i = 0 ; i< arr.length ; i++){
let obj = {}
obj['name'] = arr[i]['name']
obj['value'] = arr[i]['id']
obj['selected'] = false
items.push(obj)
if(arr[i].children !== 0){
lootIt(arr[i].children)
}
}
return items
}
评论
0赞
ganesh
10/7/2022
当我在我的 ts 文件中尝试上述代码时,它显示 .其他两个 obj 访问 ' obj['name']obj['selected']' 也相同。有没有其他方法可以做到这一点?Property 'value' does not exist on type '{}'
obj['value']
,
0赞
Kiddo
10/7/2022
@ganesh我不熟悉ts。
0赞
flyingfox
10/7/2022
#2
你可以用它来做recursion
const tree = {
"id": 1,
"name": "mainOrgName",
"children": [
{
"id": 10,
"name": "East Region",
"children": [
{
"id": 11,
"name": "test east sub 1",
"children": [
{
"id": 12,
"name": "sub 22 sub 1",
"children": [
{
"id": 15,
"name": "sub 333 of sub ",
"children": [
{
"id": 16,
"name": "sub 4444",
"children": []
}
]
}
]
}
]
}
]
},
{
"id": 13,
"name": "west region",
"children": [
{
"id": 14,
"name": "test west sub 1",
"children": []
}
]
}
]
}
function mapTree(children){
let result =[]
for(c of children){
result.push({name: c.name, value: c.id, selected: false})
result = result.concat(mapTree(c.children))
}
return result
}
console.log(mapTree(tree.children))
评论
0赞
ganesh
10/7/2022
我们是否需要添加条件,例如子项是否为空?
0赞
flyingfox
10/7/2022
@ganesh不需要,如果子是空的,那么for循环不会调用
评论