0

I have this array :

this.menuItems = [
        {
            category: 'category 4',
            items: [
                {
                    type: 'link',
                    icon: '',
                },
                {
                    type: 'link',
                    icon: '',
                },
            ]
        },
        {
            category: 'category 1',
            items: [
                {
                    type: 'text',
                    icon: 'pencil'
                },
                {
                    type: 'checkbox',
                    icon: 'pencil'
                },
            ]
        },

I want to delete the object from menuItems that have type = checkbox. I tried like this :

this.menuItems.forEach(category => {
            category.items.forEach((item, index) => {
                if (item.type === 'checkbox') {
                    category.splice(index, 1);
                }
            });
        });

But not working. Can you help me please with some advices ? Thx in advance

2
  • None of your object has type : checkbox property .. Can you recheck the data? Commented Jun 22, 2020 at 13:23
  • items got updated (there will be still some bugs, but not in the example you provided). I believe change detection is not called properly. could you provide a code where these items are rendered? Commented Jun 22, 2020 at 13:24

1 Answer 1

5
  1. map
  2. filter

const menuItems = [
    {
        category: 'category 4',
        items: [
            {
                type: 'link',
                icon: '',
            },
            {
                type: 'link',
                icon: '',
            },
        ],
    },
    {
        category: 'category 1',
        items: [
            {
                type: 'text',
                icon: 'pencil',
            },
            {
                type: 'checkbox',
                icon: 'pencil',
            },
        ],
    },
];

const result = menuItems.map(item => ({ ...item, items: item.items.filter(i => i.type !== 'checkbox') }));

console.log(result);

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

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.