Open In App

How to delete all occurrences of a particular string using Vue.js ?

Last Updated : 11 Apr, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn how to delete all occurrences of a particular word using filters in VueJS. Vue is a progressive framework for building user interfaces. Filters are a functionality provided by Vue components that let you apply formatting and transformations to any part of your template dynamic data. The filter property of the component is an object. A single filter is a function that accepts a value and returns another value. The returned value is the one that’s actually printed in the Vue.js template.

The occurrence of a particular word can be deleted by using filters on the required string. We will use the JavaScript split() method to split the string on the particular target word that has to be deleted. This will return an array that split at the points where that particular word occurs except the word that was used to split the string. Finally, we will join the array using the join() method and return the resulting string.

Example:

index.html




<html>
<head>
  <script src=
  </script>
</head>
<body>
  <div id='parent'>
    <p>
      <strong>Original String: </strong>
      {{st1}}
    <div>
      <strong>After Delete: </strong
      {{ st1 | delete('computer') }}
    </div>
    </p>
  
    <p>
      <strong>Original String: </strong>
      {{st2}}
    <div>
      <strong>After Delete: </strong
      {{ st2 | delete('language') }}
    </div>
    </p>
  
  </div>
  <script src='app.js'></script>
</body>
</html>


app.js




const parent = new Vue({
  el: '#parent',
  data: {
    st1: 'GeekforGeeks is a computer science\
          portal. computer science is the study\
          of algorithmic processes, computational\
          machines and computation itself',
    st2: 'C++ is a best language for competitive
          programming, Javascript is best\
          for scripting language',
  },
  
  filters: {
    delete: function (st, target) {
      const result = st.split(target).join('')
      return result;
    }
  }
})


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads