Vue Js Remove Element From Array

Vue js remove element from array; Through this tutorial, i am going to show you how to remove or delete elements or items from the array in Vue js.

Vue Js Remove Element From Array

Now i will show you two examples for how to remove or delete items/elements from array in vue js:

Example 1 – VueJS Remove First or Last Element From Array

Use the vue js pop and shift method to delete/remove first and last element from array; as shown below:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Js Remove Element in Array Example - laratutorials.com</title>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="bg-dark">
  <div class="container">
    <div class="col-md-6 offset-md-3">
      <div class="card mt-5">
        <div class="card-header">
          <h5 v-bind:class="red">Vue Js Remove Element in Array Reactively Example</h5>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-md-12">
              <ul>
                <li v-for="item in items" v-text="item.message"></li>
              </ul>
              <button class="btn btn-outline-primary" @click="deleteFirst">
                Delete First
              </button>
              <button class="btn btn-outline-danger" @click="deleteLast">
                Delete Last
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data:{
      items:[
           { message: 'python' },
           { message: 'C'},
           { message: 'C#'},
           { message: 'Perl'},
           { message: 'MySQL'},
      ],
    },
      methods: {
        deleteFirst() {
          this.items.shift();
          // delete last
        },
        deleteLast() {
          this.items.pop();
          // delete last
        }
      }
  })
</script>
</body>
</html>

The following code will delete/remove first and last element from array in vue js:

<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data:{
      items:[
           { message: 'python' },
           { message: 'C'},
           { message: 'C#'},
           { message: 'Perl'},
           { message: 'MySQL'},
      ],
    },
      methods: {
        deleteFirst() {
          this.items.shift();
          // delete last
        },
        deleteLast() {
          this.items.pop();
          // delete last
        }
      }
  })
</script>

Example 2 – VueJS Remove Specific Element From Array

Use the vue js splice  method to delete/remove specific item/element from array in vue js; as shown below:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Js Remove Element in Array Example</title>
  <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.0-alpha1/css/bootstrap.min.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body class="bg-dark">
  <div class="container">
    <div class="col-md-6 offset-md-3">
      <div class="card mt-5">
        <div class="card-header">
          <h5 v-bind:class="red">Vue Js Remove Element in Array Reactively Example</h5>
        </div>
        <div class="card-body">
          <div class="row">
            <div class="col-md-12">
              <ul>
                <li v-for="item in items" v-text="item.message"></li>
              </ul>
              <button class="btn btn-outline-primary" @click="deleteItem(item)">
                Delete Items
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/1.0.18/vue.min.js"></script>
<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data:{
      items:[
           { message: 'python' },
           { message: 'C'},
           { message: 'C#'},
           { message: 'Perl'},
           { message: 'MySQL'},
      ],
    },
      methods: {
      deleteItem(item) {
        this.items.splice(this.items.indexOf(item), 1);
        //remove one element starting from the element 'item'
      }
    }
  })
</script>
</body>
</html>

The following code will delete/remove specific item/element from array in vue js:

<script>
  Vue.config.devtools = true
  var app = new Vue({
    el:'.card-body',
    data:{
      items:[
           { message: 'python' },
           { message: 'C'},
           { message: 'C#'},
           { message: 'Perl'},
           { message: 'MySQL'},
      ],
    },
      methods: {
      deleteItem(item) {
        this.items.splice(this.items.indexOf(item), 1);
        //remove one element starting from the element 'item'
      }
    }
  })
</script>

Conclusion

Vue js remove element from array example tutorial. In this tutorial, you have learned how to remove/delete elements/items from array in Vue js.

Recommended VUE JS Tutorials

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *