I have a simple array like this:
const test = [{
test: 1,
key: [{
yes: true,
no: null
},{
yes: true,
no: null
},{
yes: false,
no: null
}]
},
{
test: true,
key: [{
yes: true,
no: null
}]
},
{
test: null,
key: [{
yes: null,
no: null
}]
}
];
And I want to return an array which will only include items where test is truthy (e.g. 1). And the key array inside of test will only include objects with a truthy value for key.yes.
So the expected output is:
[
{
test: 1,
key: [{yes: true, no: null},{yes: true, no: null}]
},
{
test: true,
key: [{yes: true, no: null}]
}
];
I tried to achieve this by using a simple filter like this:
const filtered = test.filter(obj => obj.test && obj.key.filter(item => item.yes).length > 0)
but this returns also the values of the key array which are false.
Any ideas why?
.filter