Home > database >  vue, match string in array without IDs and remove if matched
vue, match string in array without IDs and remove if matched

Time:10-13

I have a simple array of strings, and I'm calling a function with dropdown actions that will send a string, and if it matches one in the array, I want to remove it from the array but I'm unsure how

I've seen how I could do this with splice, but I don't have IDs on this array, it's only strings. So I literally need to match the string and remove it based on that string match

How can I simply match and remove a string from the array?

here is my code:

new Vue({
      el: "#app",
      props: { 
      },
      data: {
        tierArray:['one', 'two', 'three'],
      },
      methods: {
        removeTierSelection(value){
          console.log('this is what we are removing');
          console.log(value.name); //this prints just the string such as "one" or "two", but without the quotes
        },
      }
    })

CodePudding user response:

You can achieve it by doing this:

 const removeIndex = this.tierArray.findIndex(tier => tier === value.name)
 if (removeIndex === -1) return // means item not found

 this.tierArray.splice(removeIndex, 1)

CodePudding user response:

Another simple approach would be

this.tierArray = [...this.tierArray.filter(tier => tier !== value.name)];
  • Related