【发布时间】:2018-12-20 09:13:53
【问题描述】:
我有一个对象数组,其中包括对象数组中的子对象,我需要在父数组中查找值或在该数组的子对象中查找值等。也许是递归的。我试过这样:
var array = [
{
id: 1,
value: 'value',
children: null
},
{
id: 2,
value: 'my value',
children: [
{
id: 'child1',
value: 'my value',
children: null
},
{
id: 'child2',
value: 'value',
children: null
},
{
id: 'child3',
value: 'value,
children: [
{
id: 'childchild1',
value: 'my value',
children: null
}
]
}
]
},
{
id: 3,
value: 'value',
children: null
},
{
id: 4,
value: 'my value',
children: null
}
]
function find(searchData, target){
return target.filter((f)=>{
if(f.value.includes(searchData)){
return true
}
if(f.children){
return find(searchData, f.children)
}
})
}
find('my', array)
它返回源数组,但我需要包含文本“我的”的数组
【问题讨论】:
-
@CertainPerformance 是,但不是儿童
-
请包含您给出的示例所需的输出。
标签: javascript arrays ecmascript-6