2

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.

4
  • Will it always be only two nested arrays, or does it have to account for the fact that you might have more layers? Commented Jun 2, 2021 at 20:53
  • only two nested arrays Commented Jun 2, 2021 at 20:58
  • @CVerica Do we have a solution if we have more layers? Commented Jun 2, 2021 at 21:04
  • There's always a solution... it would just be more complicated. Commented Jun 2, 2021 at 21:56

2 Answers 2

2

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;
    })
)

Sign up to request clarification or add additional context in comments.

Comments

1

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; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.