0

below is an array of objects.

const input = [
    {
        id: '1',
        productType: [
            {
                id: '2',
                itemTypes: [
                    {
                        id: '3',
                    },
                ],
            },
        ]
        itemTypes: [
            { 
                id: '4',
            }
        ],
    },
    {
        id: '2',
        productType: [
            {
                id: '7',
                itemTypes: [
                    {
                        id: '8',
                    },
                ],
            },
            {
                id: '9',
                itemTypes: [],
            },
        ],
         
        itemTypes: [
            {
                id: '5',
            },
            {
                id: '6',
            },
        ],
    },
    {
         id: '9',
         productType: [
             {  
                 id: '11',
                 itemTypes: [],
             },
         ],
         itemTypes: [],
     },
 ]

so from above array i want to filter out the object that doesnt has itemTypes within producttypes as empty and itemTypes array empty.

so the expected output is below,

const output = [
    {
        id: '1',
        productType: [
            {
                id: '2',
                itemTypes: [
                    {
                        id: '3',
                    },
                ],
            },
        ]
        itemTypes: [
            { 
                id: '4',
            }
        ],
    },
    {
        id: '2',
        productType: [
            {
                id: '7',
                itemTypes: [
                    {
                        id: '8',
                    },
                ],
            },
            {
                id: '9',
                itemTypes: [],
            },
        ],
         
        itemTypes: [
            {
                id: '5',
            },
            {
                id: '6',
            },
        ],
    },
];

i have tried something like below,

const output = input.filter(e => e.includes(!isEmpty(productTypes.itemTypes) &&       !isEmpty(itemTypes));

but the problem above is productTypes is an array and i need to check if every object in productType has itemTypes empty array.

how can i do that. could someone help me with this. thanks.

1
  • Have you tried Array.every()? Commented Nov 29, 2022 at 12:42

1 Answer 1

2

You can try this:

const output = input.filter(
       item => item.productType.some(
         product => product.itemTypes.length > 0));
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. i want to filter the array if both the productType.itemTypes isnot empty and itemTypes is not empty

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.