'Nested filter in typescript

I have a JSON array, which looks as follows.

[
  {
        id: 1,
        name: 'Alex',
        activity: [
           {
                id: 'A1',
                status: true
            },

            {
                id: 'A2',
                status: true
            },

            {
                id: 'A3',
                status: false
            }

        ]
    },
    {
        id: 2,
        name: 'John',
        activity: [
            {
                id: 'A6',
                status: true
            },

            {
                id: 'A8',
                status: false
            },

            {
                id: 'A7',
                status: false
            }

        ]
    }
]

I want to get an array of activity id whose status should be true.I can achieve this with nester for or forEach loop. But here I am looking to achieve with the help of array functions like filter, map, and some.

I have already tried with the following.

let newArr=arr.filter(a=> a.activity.filter(b=> b.status).map(c=> c.id))

But I didn't get the correct answer Expected output

['A1','A2','A6']



Solution 1:[1]

let arr = json.flatMap(e => e.activity.filter(el => el.status).map(el => el.id))

Solution 2:[2]

let newArr=arr.map(x => x.activity)
            .reduce((acc, val) => acc.concat(val), [])
            .filter((activity:any) => activity.status)
            .map((x:any) => x.id) || [];

I got error when using flat() and flatMap().So, I have used reduce().

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 funemployed
Solution 2 Sufail Kalathil