i have an array of objects like below,
const arr_obj = [
{
id: '1',
children: [],
type: 'TYPE1',
},
{
id: '2',
children: [
{
id: '1',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '2',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '3',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
]
type: 'TYPE2',
},
{
id: '3',
children: [
{
id: '4',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '5',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '6',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
]
type: 'TYPE2',
}
]
Now i have to find out the count of type: ‘MAIN’. these ‘MAIN’ will be within type: “type2”
so the expected count is 6. the outer children array can be empty and sometimes inner children array with type: “type2” is not there at all examples like below,
children: [] //empty array
children: [
{
id: '1',
children: [],
type: 'TYPE1',
},
] //no children with type: 'TYPE2'
could someone help me with this. i am new to programming. thanks.