How to chain map and filter methods for a complex array of objects in Javascript?

I have an array as following:

var arr = [
    {                                                   
        subArray1:[
            {
                subArray2:[
                    {
                        value: 1
                    },
                    {
                        value: 0
                    }
                ]
            },
            {
                subArray2:[
                    {
                        value: 1
                    },
                    {
                        value: 0
                    }
                ]
            }
        ]
    }
];

I want to filter out all objects inside the subArray2 that contains value 1 and return the whole array. The expected output is as follows:

newArr= [
    {                                                   
        subArray1:[
            {
                subArray2:[
                    {
                        value: 1
                    }
                ]
            },
            {
                subArray2:[
                    {
                        value: 1
                    }
                ]
            }
        ]
    }
]

I am unable to chain the map and filter methods in such a way that I get the above desired output. Please help me to achieve that.

Upvotes: 2

Views: 1149

Answers (2)

Ele
Ele

Reputation: 33726

Assuming there are only those nested arrays, you can use the function reduce along with the function filter.

const arr = [    {                                                           subArray1:[            {                subArray2:[                    {                        value: 1                    },                    {                        value: 0                    }                ]            },            {                subArray2:[                    {                        value: 1                    },                    {                        value: 0                    }                ]            }        ]    }],
      result = arr.reduce((a, {subArray1}) => {
      a.push({
        subArray1: subArray1.reduce((a, {subArray2}) => {
            a.push({subArray2: subArray2.filter(({value}) => value === 1)});
            return a;
          }, [])
        });
        
        return a;
      }, []);
      
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

dave
dave

Reputation: 64657

You'll need to map each arr item, and each arr.subArray1 item, and then filter subArray2:

var arr = [
    {                                                   
        subArray1:[
            {
                subArray2:[
                    {
                        value: 1
                    },
                    {
                        value: 0
                    }
                ]
            },
            {
                subArray2:[
                    {
                        value: 1
                    },
                    {
                        value: 0
                    }
                ]
            }
        ]
    }
];

console.log(
    arr.map(({...el}) => {
        el.subArray1 = el.subArray1.map(({...el1}) => {
            el1.subArray2 = el1.subArray2.filter(({value}) => value !== 0);
            return el1;
        });
        return el;
    })
)

Upvotes: 2

Related Questions