Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

m7vm7v's avatar
Level 51

How to remove an item from a list with given ID in VueJS

Hi, I need to delete an item in list in VueJS when I have the id

My list looks like:

list : [
    {id: 5, name: Item1},
    {id: 10, name: Item2}
]

I have a method

deleteItem(itemID){}

How can I delete an item from 'list' where id = itemID

I know it shoul look something like:

this.questions.splice(id, data);

But not sure how exactly. Thanks for your help.

0 likes
11 replies
larafever's avatar

Take a look at the DOCS on collection.. You can use the collect method on any array to convert it to collection then use Laravel in built collection methods like slice and pull or where to remove by key...

https://laravel.com/docs/5.3/collections

m7vm7v's avatar
Level 51

Thanks mate but I am trying remove the object from the Vue list. I do not need to interackt with the server at this point.

m7vm7v's avatar
Level 51

$remove seems to be depricated in VueJS 2

I have tryed:

let bla = list.find( (stack) => {
                return stack.id === itemID;
            });

So when I

console.log(bla); I got the object 

But when I

this.list.splice( bla, 1);

It removes allways the first element in the list, not the selected one.

1 like
m7vm7v's avatar
Level 51

I have tryed with

delete list[bla];

and

this.list.remove(bla);

But still no success.

pittaya's avatar
pittaya
Best Answer
Level 1

when using {{splice}} the first argument is an index of array, not element itself

your code should be

let i = list.map(item => item.id).indexOf(itemID) // find index of your object
list.splice(i, 1) // remove it from array

@m7vm7v thanks. i made an update to the code.

9 likes
m7vm7v's avatar
Level 51

Thanks @pittaya That is the right one with only one difference. No need of 'bla' just '1'. Thanks alot.

mmcguire's avatar

I'm new to Vue. I got this 'delete' method to work and I was hoping there was an easier/more elegant way to do it. Req_id is the record ID that I'm getting from a form and searching for in the Reqs list.


                trash_requirement(e) {
                    if (confirm("Are you Sure?")) {                 
                        var form = $(e.target); 
                        var reqId = e.target.req_id.value;
                        
                        for (req in this.reqs) {
                            var id = this.reqs[req].id;
                            if (id == reqId) {
                                Vue.delete(this.reqs, req); 
                            }
                        }
                        
                    }
                }

Please or to participate in this conversation.