通过键从数组中删除对象

Geo*_*rge 0 typescript ionic-framework angular

我有这个数组:

this.menuItems = [
        {
            category: 'category 4',
            items: [
                {
                    type: 'link',
                    icon: '',
                },
                {
                    type: 'link',
                    icon: '',
                },
            ]
        },
        {
            category: 'category 1',
            items: [
                {
                    type: 'text',
                    icon: 'pencil'
                },
                {
                    type: 'checkbox',
                    icon: 'pencil'
                },
            ]
        },
Run Code Online (Sandbox Code Playgroud)

我想从menuItemstype = 中删除对象checkbox。我试过这样:

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

但不工作。你能帮我一些建议吗?提前谢谢

Nik*_*eev 5

  1. 地图
  2. 筛选

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);
Run Code Online (Sandbox Code Playgroud)