48

it is possible to remove specific element from lists. i tried this functions for remove element

pop() = remove last element

$remove(index) = not remove any element from lists

remove( index ) = undefined function

unshift( index ) = add new and empty element

splice( index ) = remove all element from index

please help me to remove specific element from lists.

below is my js code

var example2 = new Vue({
  el: '#example-2',
  data: {
    items: [
      { message: 'Foo' },
      { message: 'Bar' },
      { message: 'Bar1' },
      { message: 'Bar2' },
      { message: 'Bar3' },
      { message: 'Bar4' }
    ]
  },
  methods : {
    removeElement : function(index){
        this.items.$remove(index);
    }
  }
})

below is my HTML code

<ul id="example-1">
  <li v-for="(key, item) in items">
    {{ item.message }}
    <button v-on:click="removeElement(key)">remove</button>
  </li>
</ul>
1
  • 2
    @Lukasz Wiktor has the right answer for Vue version 2.2.0+. For Vue.js 2 : v-for="(key, item) in items" must be v-for="(item, index) in items" and for Vue.js 2.2.0+ you need the key attribute : <li v-for="(item, index) in items" :key="item"> github.com/vuejs/vue/releases/tag/… Commented Mar 30, 2017 at 19:23

6 Answers 6

76

$remove is deprecated in Vue.js 2.0 and replaced by splice as stated in the docs. Make sure you add the 2nd parameter of splice for it to work.

Migration From Vue 1.x - 2.0

methods: {
  removeElement: function (index) {
    this.items.splice(index, 1);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

34

You can use Vue.delete if your Vue version is 2.2.0+

Vue.delete(this.items, index);

1 Comment

I used this (Vue.delete(state.items, 'someitem')) when using the vuex store since state does not have a $delete or delete method.
24

The $.remove feature has been replaced with $.delete.

You can call it like so:

this.$delete(this.someItems, itemIndex).

It works on Object as well as Array. With objects, you need to use a keyed object. With arrays, you pass in the index of the item you want to delete.

Here is a fiddle example: https://jsfiddle.net/james2doyle/386w72nn/

EDIT

I added an example for an array as well

3 Comments

I've tried this.$delete on a Array, it makes appropriate value "undefined", but doesn't remove it from the list.
Can't use Vue developer tools on JS fiddle. But in my case it was in the Vue dev tools where I've noticed that array became something like: ['A', undefined, 'B', 'C'], after I've used this.$delete(). In the visual UI all seemed Ok, though. P.S.: with myArray.splice(index, 1) there was no such issue. I.e., I've got it right both in the UI and in the Vue Dev Tools.
this removed my warning about mutating in store state, thanks
17

$delete can use inline in @click:

<ul id="example">
   <li v-for="(item, key) in items">
       {{ item.message }}
       <button @click="$delete(items, key)">remove</button>
   </li>
</ul>

https://v2.vuejs.org/v2/api/#vm-delete

1 Comment

Thanks for that easy solution. This should be the accepted answer as this is just the smartest way to solve it imho.
1

Firstly, you should fix the methods key.

Then, you should pass the item to the $remove method, not the index. [ref]

https://jsfiddle.net/790og9w6/

Comments

1

If anyone wonders: $delete has been removed in VueJS 3 ( https://v3-migration.vuejs.org/breaking-changes/introduction.html#removed-apis ). Now it is possible to use javascripts native splice(index, count of objects to be deleted).

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.