Considering I have a nested array something like:
[
{
"uid": "j5Dc9Z",
"options": [
{
"name": 'Transportation',
"checked": true,
},
{
"name": 'Medication',
"checked": true,
}
]
},
{
"uid": "a5gdfS",
"options": [
{
"name": 'Food supply',
"checked": true,
},
]
},
{
"uid": "jHab6i",
"options": [
{
"name": "Package distri",
"checked": true,
},
{
"name": "maintainence",
"checked": false
}
]
},
{...}
]
This array is rendered as a part of dynamic checkboxes and checked values are handled on checkbox onChange function. I need to create a final separate array of the options that are only checked: true.
Something like:
[
{
"name": "Medication",
"checked": true,
},
{
"name": 'Food supply',
"checked": true,
},
{
"name": 'Transportation',
"checked": true,
}, and so on..
]
I did try following approach, but it confused on how to achieve this final array:
const category = filterOptions.filters.filter(ele => ele.options.some(option => option.checked === true)).map(item => item.options.filter(data => data.checked === true));
console.log(category);
But these returns result in the format:
0: (2) [{name: 'Transportation', checked: true}, {name: 'Medication', checked: true}]
1: [{name: 'Food Supply', checked: true}]
length: 2
__proto__: Array(0)
Please help out to resolve the same
.map()+.filter()+.flat()or.flatMap()+.filter()