我有一个看起来像这样的对象数组:
[
{
"id": "5eece32be3a4f43ca8e98688",
"title": "Main",
"children": [
{
"id": "5eece339e3a4f43ca8e98689",
"title": "Child"
}
]
},
{
"id": "5eece354e3a4f43ca8e9868a",
"title": "Another",
"children": [
{
"id": "5eece362e3a4f43ca8e9868b",
"title": "Child 2",
"children": [
{
"id": "5eece36ee3a4f43ca8e9868c",
"title": "Child 3"
}
]
},
{
"id": "5eece9210a445e2d0ca78978",
"title": "Temp"
}
]
}
]
我需要从每个主要对象和子对象中提取id
。可以有任意数量的嵌套子代。它可以下降到多个级别,因此这需要能够适应对象具有的所有子代。我尝试映射它,并将id拉到一个新数组,但这只给了我2个主要对象。有没有一种方法可以创建包含所有id
的数组。
类似这样的东西:
["5eece32be3a4f43ca8e98688", "5eece339e3a4f43ca8e98689", "5eece354e3a4f43ca8e9868a", "5eece362e3a4f43ca8e9868b", "5eece36ee3a4f43ca8e9868c", "5eece9210a445e2d0ca78978"]