[Vuejs]-Remove an item from array by id

0👍

Why not use an object instead of an array, something like this:

data: function(){
            return {
            items: [
            { id: '1', name: 'Item 1', bool: false},
            { id: '2', name: 'Item 2', bool: false},
            { id: '3', name: 'Item 3', bool: false},
            { id: '4', name: 'Item 4', bool: false}
            ],
            checkedItemsObj: {},

        };
    },

    methods: 
    {
        select: function(event, index) {
            let item = this.items[index];

            if (!item.bool) {                    
                this.checkedItemsObj[item.id] = item 
            } else { 
                delete this.checkedItemsObj[item.id]  
            }
        },

        getCheckedItems: () => Object.values(data().checkedItemsObj)
    }

Now to get the array of checked items anytime, you can call methods.getCheckedItems()

Leave a comment