Vue Js Remove Item From Array Example

Vue Js Remove Item From Array Example

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

This tutorial will guide you step by step on how to vue js remove/delete element/item from array in vue js. And as well as, make simple examples of how to vue js remove/delete element/item from array in vue js.

Vue Js Delete Element From Array

This tutorial will show you 2 examples of how to remove or delete items/elements from array in vue js:

Example 1 – VueJS Remove First or Last Element From Array

This example uses the standard pop and shift method to delete/remove first and last element from array in vue js:

<!DOCTYPE html>
<html>
<head>
  <title>Vue Js Remove Element in Array Example - tutsmake.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

This example uses the standard splice  method to delete/remove specific item/element from array in vue js:

<!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

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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