I try to filter the hole Object (objB) out of an Array, which is matching a Value (titleB2) of a Property.
const array = [
{
name: 'objA',
subArray: []
}, {
name: 'objB',
subArray: [
{
title: 'titleB'
}, {
title: 'titleB2'
}, {
title: 'titleB3'
}
]
}, {
name: 'objC',
subArray: [
{
title: 'titleC'
}, {
title: 'titleC2'
}, {
title: 'titleC3'
}
]
}, {
name: 'objD',
subArray: []
}
]
const filterArray = array.filter(a => a.subArray.length > 0);
console.log(filterArray);
// Output: objB + objC
const resArray = filterArray.filter(a => a.subArray.filter(f => f.title === 'titleB2'));
console.log(resArray);
// Output: objB + objC
I guess, I do something logically wrong. But what exactly?
I need the output
{
name: 'objB',
subArray: [
{
title: 'titleB'
}, {
title: 'titleB2'
}, {
title: 'titleB3'
}
]
}
I can imagine I use the filter filterArray.filter() wrong, because it already filtered? But I do this because I have multiple arrays in one array. Well, I am not realy sure, to be honest.
But what I dont understand is, why can I use the condition a => a.subArray.length > 0? but not f => f.title === 'titleB2'